CertGrid CertGrid
Hands-on Lab·Python Automation for IT

Python subprocess Output and Return Codes

Running a command is half the job; reacting to what it did is the other half. `check=True` turns a non-zero exit into an exception carrying both streams, `text=True` decides whether you get `str` or `bytes`, and `input=` feeds a command on standard input without a temporary file. All four decisions matter in a script nobody is watching.

OS and Process Automation Guide 18 of 39 Intermediate

Written against the versions above. `capture_output=True` needs Python 3.7; before that it was `stdout=PIPE, stderr=PIPE`. `text=True` and its older spelling `universal_newlines=True` are the same argument.

Everything on this page runs on the control node. Any machine with Python 3 will do.
Server NameIP AddressOSRolesCPURAMHDD
RUNNER01192.168.0.27Ubuntu 26.04 LTSControl node - every script in this path runs here2 Core4 GB50 GB

Before you start

  1. check=True, so a failure is an exception

  2. text against bytes

  3. Feeding something on standard input

  4. The shape to copy

Official sources