subprocess.check_output
Prev: subprocess.check_call | Up: subprocess | Next: subprocess.CompletedProcess
Rel: subprocess.call, subprocess.CalledProcessError, subprocess.check_call, subprocess.CompletedProcess
Ref: docs.python.org — subprocess.check_output
Tags: #public #python3 #stdlib #subprocess
Import
import subprocess
from subprocess import check_output
Signature & Overview
subprocess.check_output(*popenargs, timeout=None, **kwargs)
Run command with arguments and return its output.
Usage
import subprocess
import sys
out = subprocess.check_output([sys.executable, "-c", "print('hi')"], text=True)
assert out.strip() == "hi"
Gotchas
- Returns
bytesby default: Passtext=True(orencoding="utf-8") to get astr. - Modern equivalent:
subprocess.run(..., check=True, capture_output=True, text=True).stdoutalso capturesstderrcleanly.
REPL Inspect
from subprocess import check_output
help(check_output)
Linked from
- tuts/python3/stdlib_module/subprocess
- tuts/python3/stdlib_module/subprocess/call
- tuts/python3/stdlib_module/subprocess/CalledProcessError
- tuts/python3/stdlib_module/subprocess/check_call
- tuts/python3/stdlib_module/subprocess/CompletedProcess
- tuts/python3/stdlib_module/subprocess/getoutput
- tuts/python3/stdlib_module/subprocess/getstatusoutput
- tuts/python3/stdlib_module/subprocess/Popen
- tuts/python3/stdlib_module/subprocess/run
- tuts/python3/stdlib_module/subprocess/SubprocessError
- tuts/python3/stdlib_module/subprocess/TimeoutExpired
Related notes
- tuts/python3/stdlib_module/subprocess/call Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CalledProcessError Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_call Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CompletedProcess 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/getoutput links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/getstatusoutput links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/Popen links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/run links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/SubprocessError links to this page; shared tags: #stdlib, #subprocess