Python: Execute Shell Command

Shell

Execute Command

1

The quickest way is to use os module:

1import os
2ret_code = os.system("docker-compose")

It returns exit code as int. There is no way to capture program output other than looking at it in the console.

2

If you need to get an output:

1import os
2output: str = os.popen("docker-compose").read()

When command fails, the output is still returned as an empty string, but there’s no way to tell whether it is an empty output or the command has failed.

Have feedback or questions? Feel free to email me.