On this page
subprocess.getoutput
Prev: subprocess.CompletedProcess | Up: subprocess | Next: subprocess.getstatusoutput
Rel: subprocess.call, subprocess.CalledProcessError, subprocess.check_call, subprocess.check_output
Ref: docs.python.org — subprocess.getoutput
Tags: #public #python3 #stdlib #subprocess
Import
import subprocess
from subprocess import getoutput
Signature & Overview
subprocess.getoutput(cmd, *, encoding=None, errors=None)
Return output (stdout or stderr) of executing cmd in a shell.
Usage
import subprocess
out = subprocess.getoutput("echo hello")
assert out == "hello"
Gotchas
- Runs through the system shell and discards the exit code: Never pass untrusted input into
getoutput()(shell injection risk), and note that a failing command still returns its text silently. - Prefer
subprocess.run([...], capture_output=True, text=True, check=True)for production code.
REPL Inspect
from subprocess import getoutput
help(getoutput)
Linked from
Related notes
- tuts/python3/stdlib_module/subprocess/CompletedProcess Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/getstatusoutput Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/subprocess/call Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CalledProcessError Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_call Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_output Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/Popen shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/run shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/SubprocessError shared tags: #stdlib, #subprocess