Python Glossary Русский

Python 3.0+subprocessfunction

subprocess.getstatusoutput()

shell invocation

Runs a command string through the shell and returns an (exit status, output) tuple; a leftover of Python 2's commands module.

Syntax

status, output = subprocess.getstatusoutput(cmd, *, encoding=None, errors=None)

The tuple is ordered code first, text second, and nothing is raised on failure: you must test the code yourself. stderr is merged into stdout, so error text lands in the same output. The string goes through a shell (sh or cmd.exe), so quoting and variables behave differently across systems. New code is better off with run() and an argument list — no shell, no shell pitfalls.

Examples

import subprocess, sys
cmd = f'"{sys.executable}" -c "print(1); raise SystemExit(3)"'
status, out = subprocess.getstatusoutput(cmd)
print(status, out)  # → 3 1
import subprocess
print(subprocess.getstatusoutput('no-such-program-xyz'))  # → ? (127, '/bin/sh: 1: no-such-program-xyz: not found') — текст и код зависят от оболочки

See also

Open in the full glossarydocs.python.orgReport an inaccuracy