Complete guide to Python's os.system function covering shell command execution, return values, and practical examples.
Last modified April 11, 2025
This comprehensive guide explores Python’s os.system function, which executes shell commands. We’ll cover command execution, return values, security considerations, and practical examples.
The os.system function executes a command in a subshell. It takes a string command and returns the exit status of the process.
Key characteristics: blocks until completion, uses system shell (/bin/sh on Unix, cmd.exe on Windows), returns 0 on success, non-zero on failure.
The most basic usage of os.system runs a single shell command. This example demonstrates executing the ’ls’ command on Unix-like systems.
simple_command.py
import os
return_code = os.system(“ls -l”)
print(f"Command returned: {return_code}")
if return_code == 0: print(“Command executed successfully”) else: print(“Command failed”)
This runs ’ls -l’ in the shell and captures the return code. A return code of 0 typically indicates success, while non-zero indicates failure.
Note that the command output goes directly to stdout, not captured by Python. For output capture, consider subprocess.run instead.
os.system can execute any system command available in the shell. This example shows system information commands on different platforms.
system_commands.py
import os import platform
system = platform.system()
if system == “Linux”: os.system(“uname -a”) os.system(“df -h”) elif system == “Windows”: os.system(“systeminfo”) os.system(“wmic diskdrive get size”) elif system == “Darwin”: os.system(“sw_vers”) os.system(“diskutil list”) else: print(“Unsupported system”)
This script detects the operating system and runs appropriate system commands. The output appears directly in the console where Python is running.
Platform detection helps write cross-platform scripts that adapt to different operating systems automatically.
os.system can interact with the filesystem using shell commands. This example demonstrates file creation, copying, and deletion.
file_management.py
import os
os.system(“echo ‘Hello World’ > test.txt”)
os.system(“cp test.txt test_copy.txt”)
os.system(“ls -l *.txt”)
os.system(“rm test.txt test_copy.txt”)
os.system(“ls -l *.txt || echo ‘No text files found’”)
This script performs basic file operations using shell commands. Each command runs sequentially, with the next starting only after the previous completes.
While Python has built-in file operations, shell commands can be useful for complex operations or when integrating with existing shell scripts.
Shell commands can use environment variables, which can be set from Python. This example shows how to pass Python variables to shell commands.
environment_vars.py
import os
username = “testuser” log_dir = “/var/log”
os.system(f"echo ‘Processing logs for {username}’") os.system(f"ls -l {log_dir} | head -n 5")
os.environ[“TEMP_DIR”] = “/tmp” os.system(’echo “Temporary directory is $TEMP_DIR”’)
file_count = 5 os.system(f"for i in $(seq 1 {file_count}); do touch file_$i.txt; done") os.system(“ls file_*.txt”)
Python variables can be interpolated into shell commands using f-strings. Environment variables set in Python are available to the subshell.
Be cautious with variable interpolation to avoid shell injection vulnerabilities. Sanitize any user-provided input.
Shell features like command chaining and pipelines work with os.system. This example demonstrates combining multiple commands.
command_chaining.py
import os
os.system(“date && echo ‘Command successful’ || echo ‘Command failed’”)
os.system(“ps aux | grep python | head -n 5”)
os.system(“grep -r ‘import os’ . 2>/dev/null | wc -l > count.txt”)
with open(“count.txt”) as f: print(f"Found {f.read().strip()} occurrences")
os.system(“rm count.txt”)
This shows how to use shell features like && (AND), || (OR), and | (pipeline). Output redirection works as in normal shell usage.
Command chaining can make complex operations concise, but may reduce readability for those unfamiliar with shell syntax.
While os.system doesn’t capture output, we can redirect to files. This example shows techniques for working with command output.
command_output.py
import os
os.system(“ls -l / > dir_listing.txt”)
with open(“dir_listing.txt”) as f: print(“Directory listing:”) print(f.read())
import tempfile with tempfile.NamedTemporaryFile() as tmp: os.system(f"uname -a > {tmp.name}") print("\nSystem info:") print(tmp.read().decode())
os.system(“ls nonexistent_dir 2> errors.txt”) with open(“errors.txt”) as f: print("\nErrors:") print(f.read())
Output can be captured by redirecting to files, then reading those files. Temporary files provide a cleaner approach than manually managing files.
For more advanced output handling, consider subprocess.Popen or subprocess.run.
Shell injection: Avoid unsanitized user input in commands
Platform differences: Commands may behave differently across OS
Error handling: Check return codes for command success
Alternatives: Prefer subprocess for more control
Environment: Commands run in subshell with current environment
Input sanitization: Always sanitize dynamic command parts
Return code checking: Verify commands succeeded
Platform awareness: Test commands on target platforms
Error handling: Handle potential command failures
Documentation: Document shell dependencies clearly
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all Python tutorials.