all Python 3subprocessfunction
subprocess.check_call()
running processes
Runs a command, waits for it and raises CalledProcessError on a non-zero exit status; on success it returns 0.
Syntax
subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False, cwd=None, timeout=None)
A common mistake is expecting check_call to return the command's output: it only returns 0, and the output goes straight to the console. For the output use check_output() or run(check=True, capture_output=True). stdout=PIPE is as dangerous here as in call: nobody reads the pipe, so a chatty child deadlocks. New code spells the same thing run(..., check=True).
Examples
import subprocess, sys
rc = subprocess.check_call([sys.executable, '-c', 'print(42)'])
print(rc) # → 42 / 0 — 42 печатает сам дочерний процесс
import subprocess, sys
try:
subprocess.check_call([sys.executable, '-c', 'import sys; sys.exit(5)'])
except subprocess.CalledProcessError as e:
print(e.returncode, e.output) # → 5 None
See also
Common errors
Open in the full glossarydocs.python.orgReport an inaccuracy