Python subprocess
Module: Detailed Overview and Examples
The subprocess
module in Python is used to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module is particularly useful for running shell commands, interacting with other programs, and managing processes.
Importing the subprocess
Module
To use the subprocess
module, you need to import it:
Running External Commands
Basic Usage
Example: Running a Simple Command
import subprocess
# Run a simple command
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
# Print the command's output
print(result.stdout)
Communicating with Processes
Sending Input to a Process
Example: Sending Input via stdin
import subprocess
# Run a command that reads input
proc = subprocess.Popen(['cat'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, text=True)
# Send input and get the output
output, _ = proc.communicate('Hello, World!')
print(output)
Capturing Output and Error Streams
Capturing Output and Errors
Example: Capturing Both Output and Errors
import subprocess
# Run a command and capture its output and errors
result = subprocess.run(['ls', '-l'], capture_output=True, text=True)
# Print the command's output
print('Output:')
print(result.stdout)
# Print the command's error (if any)
print('Error:')
print(result.stderr)
Handling Errors
Example: Running a Command That Fails
import subprocess
# Run a command that fails
try:
result = subprocess.run(['ls', 'nonexistentfile'], capture_output=True, text=True, check=True)
except subprocess.CalledProcessError as e:
print('Command failed with return code', e.returncode)
print('Error output:', e.stderr)
Process Management
Running Commands in Background
Example: Running a Command in Background
import subprocess
# Run a command in background
proc = subprocess.Popen(['sleep', '10'])
# Check if the process is still running
print('Process is running:', proc.poll() is None)
# Wait for the process to complete
proc.wait()
print('Process finished')
Using Shell Commands
Running Commands in Shell
Example: Running a Command with Shell=True
import subprocess
# Run a shell command
result = subprocess.run('echo $HOME', shell=True, capture_output=True, text=True)
# Print the command's output
print(result.stdout)
Avoiding Shell Injection
Example: Avoiding Shell Injection by Using List Arguments
import subprocess
# Use list arguments instead of shell=True
result = subprocess.run(['echo', '$HOME'], capture_output=True, text=True)
# Print the command's output
print(result.stdout) # Output will not include the environment variable
Advanced Features
Using subprocess.Popen
for More Control
Example: Using subprocess.Popen
for Streaming Output
import subprocess
# Run a command and stream its output
proc = subprocess.Popen(['ping', 'localhost'], stdout=subprocess.PIPE, text=True)
# Print the output line by line
for line in proc.stdout:
print(line, end='')
Setting Environment Variables
Example: Modifying Environment Variables
import subprocess
import os
# Define new environment variables
env = os.environ.copy()
env['MY_VAR'] = 'value'
# Run a command with modified environment
result = subprocess.run(['printenv', 'MY_VAR'], capture_output=True, text=True, env=env)
# Print the command's output
print(result.stdout.strip())
Redirecting Output to Files
Example: Redirecting Output to a File
import subprocess
# Run a command and redirect its output to a file
with open('output.txt', 'w') as f:
subprocess.run(['ls', '-l'], stdout=f)
Practical Examples
Example 1: Running a System Command and Parsing Output
import subprocess
# Run a system command
result = subprocess.run(['df', '-h'], capture_output=True, text=True)
# Print and parse the output
for line in result.stdout.splitlines():
print(line)
Example 2: Downloading a File Using curl
import subprocess
# Download a file using curl
url = 'http://example.com/file.txt'
filename = 'file.txt'
subprocess.run(['curl', '-o', filename, url])
print(f'Downloaded {filename}')
Example 3: Running a Python Script
import subprocess
# Run a Python script
result = subprocess.run(['python3', 'script.py'], capture_output=True, text=True)
# Print the script's output
print(result.stdout)
Conclusion
The subprocess
module provides a robust interface for running and interacting with external processes. By using functions such as subprocess.run()
, subprocess.Popen()
, and handling process input/output, you can perform a wide range of tasks from simple command execution to complex process management. Understanding how to effectively use subprocess
will enable you to integrate and automate system-level tasks within your Python programs.