Python: Execute Shell Command


Execute Command


The quickest way is to use os module:

import os
ret_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.


If you need to get an output:

import os
output: 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 a question⁉ Contact me.