Preface
In Python programming, external commands or scripts are often required. In Python standard librarysubprocess
The module provides a wealth of features that allow you to start new processes, connect to their input/output/error pipelines, and get their return codes. This article will introduce in detailsubprocess
How to use modules, including basic usage, advanced functions and some precautions.
1. Basic usage
1.1 Use()
()
It is an advanced interface introduced in Python 3.5 and above to run child processes and wait for them to complete. It returns aCompletedProcess
An example containing the process's return code, standard output, and standard error output.
import subprocess result = (['ls', '-l'], capture_output=True, text=True) print(f'Return code: {}') print(f'Output:\n{}') print(f'Error:\n{}')
-
capture_output=True
: Capture standard output and standard error output. -
text=True
: Decode the output into a string (available in Python 3.7 and later, the previous version requires manual decoding).
1.2 Using ()
()
Provides a more flexible way to start and manage subprocesses. It returns aPopen
Objects that allow you to interact more complexly with child processes.
import subprocess process = (['ls', '-l'], stdout=, stderr=, text=True) stdout, stderr = () print(f'Return code: {}') print(f'Output:\n{stdout}') print(f'Error:\n{stderr}')
-
stdout=
andstderr=
: Redirects standard output and standard error output into the pipeline for subsequent reads. -
communicate()
: Wait for the process to end and get the standard output and standard error output.
2. Advanced functions
2.1 Manage input and output
You can passPopen
The object'sstdin
、stdout
andstderr
The attributes interact with the child process.
import subprocess process = (['grep', 'pattern'], stdin=, stdout=, text=True) output, error = (input='line with pattern\nanother line\n') print(f'Return code: {}') print(f'Output:\n{output}') print(f'Error:\n{error}')
-
input
Parameters: to the child processstdin
Write data.
2.2 Set environment variables
You can passenv
Parameters set environment variables for child processes.
import subprocess import os env = () env['MY_VAR'] = 'my_value' result = (['printenv', 'MY_VAR'], env=env, capture_output=True, text=True) print()
2.3 Capture the output of the child process without blocking
You can usePopen
The object'sstdout
andstderr
Filedreadline()
orread()
Method to read the output step by step, rather than waiting for all outputs to complete at once.
import subprocess process = (['ls', '-l', '/some/large/directory'], stdout=, stderr=, text=True) while True: line = () if not line: break print(()) () # Wait for the process to end
3. Things to note
Security: Avoid executing untrusted input directly to prevent command injection attacks. Use list-form commands and parameters instead of string splicing.
Cross-platform compatibility: Commands and paths on different operating systems may vary. Make sure your code is tested on the target platform.
Resource Management: Make sure to close the pipeline and file descriptors of the child process when it is no longer needed to avoid resource leakage.
Error handling: Check the return code of the child process and process the error information in the standard error output as needed.
4. Summary
subprocess
Modules are powerful tools in Python for handling external commands and scripts. pass()
and()
, you can start and manage child processes in a flexible and powerful way. Mastering these tools will enable you to write more complex and robust Python programs.
This is the article about the usage and precautions of subprocess module in Python. For more detailed explanations of subprocess module in Python, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!