subprocess.
What It Does and Why It Matters
subprocess.run(command_arguments, check=..., capture_output=..., text=..., timeout=...) executes a command and waits for completion. Pass an argument list without shell=True for most applications.
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
completed_process = subprocess.run(
[sys.executable, '-c', "print('child ready')"],
capture_output=True, text=True, check=True, timeout=5
)
print(completed_process.stdout.strip())
How the Code Works
- The imports make these standard-library tools available: subprocess, sys.
- The name completed_process receives the value of subprocess.run([sys.executable, '-c', "print('child ready')"], capture_output=True.... 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
Run the current Python interpreter to print one line safely.
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
- Joining user input into a shell command string or omitting a timeout for external work.
- Treating successful output as proof of a successful exit code, or accepting an unchecked path or command argument.
Key Takeaways
- subprocess.
- subprocess.run(command_arguments, check=..., capture_output=..., text=..., timeout=...) executes a command and waits for completion.
- Run the current Python interpreter to print one line safely.
- Avoid shell=True with untrusted input. Prefer an explicit list of arguments and a timeout.