Python 3.5+subprocessfunction
subprocess.run()
running processes
Runs a command, waits for it to finish and returns a CompletedProcess. The main way to call external programs since Python 3.5.
Syntax
subprocess.run(args, *, capture_output=False, text=False, check=False, timeout=None, ...)
check=False is the default, so a command that failed still looks like a successful call — either test result.returncode yourself or pass check=True, otherwise the failure slips by unnoticed. Without capture_output=True the output goes straight to the console and result.stdout stays None. Pass the arguments as a list: a single string plus shell=True turns any user-supplied text into a command-injection hole.
Examples
import subprocess
# простой запуск
result = subprocess.run(['ls', '-la'], capture_output=True, text=True)
print(result.stdout)
import subprocess
# проверка кода возврата
subprocess.run(['python', '--version'], check=True) # CalledProcessError при ошибке
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy