| [ Web Proxy ] |
| Viewing: https://www.pythonbyexample.dev/examples/subprocesses | [Back] [Original] |
Standard Library
subprocess.run() spawns a child Python interpreter and waits for it: capture_output=True stores the child's stdout and stderr on the result, text=True decodes them as strings, and check=True raises CalledProcessError on a non-zero exit. The result object carries the captured streams and exit code as portable evidence the child ran. The in-browser Run button cannot spawn processes, so pressing Run here fails in the sandbox; the output below was produced by really spawning the child under standard CPython when the example was verified.
Source
import subprocess
import sys
result = subprocess.run(
[sys.executable, "-c", "print('child process')"],
text=True,
capture_output=True,
check=True,
)
print(result.stdout.strip())
print(result.returncode)Output
child process
0Notes
check=True turns non-zero exits into exceptions.child process
0
Execution time appears here after you run the example.
| Web Proxy Viewer | New URL | Original Page |