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
- Python3.14.4
- Control nodeUbuntu 26.04 LTS
- Managed hostsRHEL 10.0
- requests2.34.2
- paramiko5.0.0
- pytest9.1.1
- PyYAML6.0.3
- boto3 / botocore1.43.78
- TimeAbout 15 min
- Reviewed24 August 2026
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.
| Server Name | IP Address | OS | Roles | CPU | RAM | HDD |
|---|---|---|---|---|---|---|
| RUNNER01 | 192.168.0.27 | Ubuntu 26.04 LTS | Control node - every script in this path runs here | 2 Core | 4 GB | 50 GB |
Before you start
-
check=True, so a failure is an exception
-
text against bytes
-
Feeding something on standard input
-
The shape to copy