Content · Programming · Python

Python subprocess.check_output() Function: Usage and Examples

subprocess.

2 views Published Oct 17, 2026 Updated Oct 18, 2026 Admin

subprocess.

What It Does and Why It Matters


subprocess.check_output() captures standard output and raises CalledProcessError for a nonzero exit code. It returns bytes unless text=True or an encoding is set.

Avoid shell=True with untrusted input. Prefer an explicit list of arguments and a timeout.

Step-by-Step Example


Start with the complete example below. Read the inputs and predict what the final print statement will show before you run it.

import subprocess
import sys
output_text = subprocess.check_output(
    [sys.executable, '-c', "print('checked output')"],
    text=True, timeout=5
)
print(output_text.strip())

How the Code Works


  • The imports make these standard-library tools available: subprocess, sys.
  • The name output_text receives the value of subprocess.check_output([sys.executable, '-c', "print('checked output')"], text=Tr.... Changing this input lets you explore a different case.
  • The print calls display the results. Compare these results with the method’s documented return type and the input used.
  • The command arguments are represented as separate list elements instead of being assembled into shell text. This limits accidental quoting mistakes and reduces injection risk when shell=False.

This example depends on operating-system or command-line behavior. Try it in your own terminal after reviewing the relevant permissions and environment assumptions.

What to Notice


Pay attention to the accepted input types, the returned value and the boundary behavior. Predict what changes if you replace the example input with an empty, zero, negative or missing value, where that value is meaningful for this operation.

If the task uses a library function, inspect its documented exceptions and return type before reusing it inside a larger function.

Where You Would Use This


This is useful for scripts, maintenance commands and automated workflows. Treat inputs as untrusted, keep normal results separate from errors, and choose exit codes consistently. Keep reusable logic outside the command-line parser so it can be tested independently.

Practice Exercise


Capture one line of output from the current Python interpreter.

Before running your edited example, write down the result you expect. Then test at least one valid boundary or deliberately invalid value, using appropriate exception handling where necessary.

Reference: official Python documentation for the complete API contract and version notes.

Common Beginner Mistakes


  • Expecting check_output() to return a CompletedProcess object or silently ignoring a raised error status.
  • Overlooking the documented condition: subprocess.check_output() captures standard output and raises CalledProcessError for a nonzero exit code.

Key Takeaways


  • subprocess.
  • subprocess.check_output() captures standard output and raises CalledProcessError for a nonzero exit code.
  • Capture one line of output from the current Python interpreter.
  • Avoid shell=True with untrusted input. Prefer an explicit list of arguments and a timeout.
Share this resourceSend this page to someone who may find it useful.