← Back to Articles
Tutorial

Understanding Python's Subprocess Module

Learn how to use Python's subprocess module for running shell commands.

Python's subprocess module is a powerful tool for executing shell commands and managing subprocesses directly from your Python code. It allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes.

The subprocess module provides several methods, including subprocess.run and subprocess.Popen. For example, subprocess.run(['ls', '-l']) executes the 'ls -l' command in a Unix-like system and waits for it to complete. The subprocess.Popen class offers more control over the command execution, like redirecting input and output.

When using subprocess, it's important to handle exceptions such as subprocess.CalledProcessError when a command fails. Also, avoid using shell=True unless necessary, as it can introduce security risks. Always validate and sanitize input if you must use shell=True.

A common mistake is neglecting to manage subprocess timeouts, which can lead to hanging processes. Use the timeout parameter with subprocess.run to prevent this. Additionally, ensure that input/output streams are properly closed to avoid resource leaks.

Code Examples

Example 1

import subprocess
result = subprocess.run(['echo', 'Hello, World!'], capture_output=True, text=True)
print(result.stdout)

Example 2

import subprocess
process = subprocess.Popen(['ls', '-l'], stdout=subprocess.PIPE)
output, errors = process.communicate()
print(output.decode())

More Python Tutorials