subprocess
subprocess modules allows you to spawn new processes, interact with file descriptors, and obtain exit codes. The recommended approach is to use the run()
function as default, which runs a CLI command with options as a list of strings and returns a CompletedProcess
instance.\
Execute shell command
Unlike os.system
, subprocess.run()
takes a list of arguments.
subprocess.run(['ls','-l,'.'], 0)
capture_output
to True
to save output, stored as property stdout
of the returned object.
data = subprocess.run(['ls,'-l','.'], 0, capture_output=True)
data.stdout.decode('utf-8')
CompletedProcess
instance with the command's output stored under the stdout
property
subprocess.run(['ls','-l','/dev/null'], capture_output=True)
CalledProcessError
exception because of the non-zero exit code
subprocess.run('exit 1', shell=True, check=True)