Assign output of os.system to a variable and prevent it from being displayed on the screen
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
os.system is the wrong tool if you want command output in a Python variable. It only returns the command's exit status, while the command's standard output and standard error still go to the terminal unless you redirect them manually.
Why os.system Does Not Capture Output
This code returns an integer status, not the printed text:
You will see hello on the screen because the child process writes directly to the terminal. The variable status only contains the process exit code.
So if your real requirement is:
- capture command output
- suppress display on the screen
- inspect return code cleanly
then subprocess is the modern answer.
Use subprocess.run to Capture Output
The most direct replacement is subprocess.run with capture_output=True and text=True:
Nothing is printed unless you print it yourself. That is already a better fit than os.system.
Suppress Output Completely
If you do not need the text at all and only care whether the command succeeded, send both streams to DEVNULL:
This is cleaner than putting shell redirection syntax into a command string.
Capture Standard Output and Standard Error Separately
Real programs often need to keep output for later inspection:
Keeping the two streams separate is usually better than blindly mixing them.
Prefer Argument Lists to Shell Strings
When you do not need shell features such as pipes or globbing, pass the command as a list:
This avoids many quoting bugs and reduces shell-injection risk. Use shell=True only when you actually need shell syntax and understand the security tradeoff.
Timeouts and Error Handling
One reason subprocess is better is that it handles operational problems more explicitly:
That is much easier to reason about than a shell string handed to os.system.
When check_output Is Enough
If you only want standard output and want Python to raise automatically on non-zero exit status, subprocess.check_output is a compact option:
It is less flexible than subprocess.run, but useful for small scripts.
Common Pitfalls
- Expecting
os.systemto return the command's printed output instead of only the exit status. - Using shell redirection strings when
subprocessalready provides explicit output controls. - Suppressing both stdout and stderr too early and losing useful debugging information.
- Passing a single shell string when a list of arguments would be safer and clearer.
- Forgetting timeout handling for commands that may hang.
Summary
- '
os.systemreturns a status code, not the command output text.' - Use
subprocess.runto capture output into variables. - Use
DEVNULLif you want the command to stay quiet on screen. - Prefer list-based arguments over shell strings when you do not need shell features.
- Add timeout and error handling if the command is part of real application logic.

