subprocess
module to run external processes and interact with their standard input, output, and error streams. If you want to capture the standard output (stdout) of a subprocess, you can do so by using the subprocess.run()
function or the subprocess.Popen
class.
Here’s how you can capture and handle stdout in both cases:
Method 1: Using subprocess.run()
The subprocess.run()
function allows you to run a command and capture its output. Here’s an example of how to use it to capture stdout:
import subprocess
# Define the command you want to run
command = ["ls", "-l"]
# Run the command and capture the stdout
result = subprocess.run(command, stdout=subprocess.PIPE, text=True)
# Check if the command was successful
if result.returncode == 0:
# Print the captured stdout
print("Standard Output:")
print(result.stdout)
else:
print("Error:", result.returncode)
Code language: Python (python)
In this example, the stdout=subprocess.PIPE
argument tells subprocess.run()
to capture the stdout of the command, and text=True
indicates that the output should be treated as text.
Method 2: Using subprocess.Popen
The subprocess.Popen
class allows for more fine-grained control over the subprocess, and you can capture stdout while the process is running. Here’s an example:
import subprocess
# Define the command you want to run
command = ["ls", "-l"]
# Create a Popen object to run the command
process = subprocess.Popen(command, stdout=subprocess.PIPE, text=True)
# Read and print stdout while the process is running
print("Standard Output:")
for line in process.stdout:
print(line, end="")
# Wait for the process to complete and get the return code
return_code = process.wait()
print("Return Code:", return_code)
Code language: Python (python)
In this example, process.stdout
is an iterable that allows you to read the stdout line by line as the subprocess runs.
Choose the method that best suits your needs based on whether you want to capture the output after the subprocess has completed (subprocess.run()
) or while it’s running (subprocess.Popen
).
Subprocess Python Stdout To String
To capture the standard output (stdout) of a subprocess in Python and store it as a string, you can use the subprocess.run()
function and then decode the captured bytes to a string. Here’s how to do it:
import subprocess
# Define the command you want to run
command = ["ls", "-l"]
# Run the command and capture the stdout as bytes
result = subprocess.run(command, stdout=subprocess.PIPE)
# Check if the command was successful
if result.returncode == 0:
# Decode the captured stdout to a string using UTF-8 encoding
captured_stdout = result.stdout.decode("utf-8")
# Print the captured stdout
print("Standard Output:")
print(captured_stdout)
else:
print("Error:", result.returncode)
Code language: Python (python)
In this example, stdout=subprocess.PIPE
tells subprocess.run()
to capture the stdout of the command as bytes. We then decode these bytes to a string using the UTF-8 encoding.
Make sure to specify the correct encoding (in this case, UTF-8) based on the encoding of the output you expect from the subprocess.
Subprocess Python Stdout To File
To capture the standard output (stdout) of a subprocess in Python and write it to a file, you can use the subprocess.run()
function and redirect the output to a file. Here’s how to do it:
import subprocess
# Define the command you want to run
command = ["ls", "-l"]
# Specify the output file
output_file = "output.txt"
# Run the command and redirect stdout to the output file
with open(output_file, "w") as file:
result = subprocess.run(command, stdout=file, text=True)
# Check if the command was successful
if result.returncode == 0:
print("Command executed successfully.")
else:
print("Error:", result.returncode)
Code language: Python (python)
In this example:
stdout=file
redirects the standard output of the subprocess to the specified output file, opened in write mode (“w”).text=True
indicates that the output should be treated as text.
After running this code, the output of the subprocess will be written to the output.txt
file.
Make sure to replace "ls -l"
with the actual command you want to run, and "output.txt"
with the desired output file path.
Read Python Subprocess Stdout Line By Line
To read the standard output (stdout) of a subprocess line by line in Python, you can use the subprocess.Popen
class and a loop to iterate through the lines of stdout. Here’s an example:
import subprocess
# Define the command you want to run
command = ["ls", "-l"]
# Create a Popen object to run the command
process = subprocess.Popen(command, stdout=subprocess.PIPE, text=True)
# Read and print stdout line by line
print("Standard Output:")
for line in process.stdout:
print(line, end="") # Use end="" to prevent adding extra newlines
# Wait for the process to complete and get the return code
return_code = process.wait()
print("Return Code:", return_code)
Code language: Python (python)
In this example:
- We use
subprocess.Popen
to start the subprocess with the specified command. stdout=subprocess.PIPE
tells the subprocess to capture its stdout.text=True
indicates that the output should be treated as text.
We then iterate through the lines of stdout using a for
loop, printing each line. The end=""
argument in the print
function is used to prevent adding extra newlines when printing each line.
Finally, we wait for the process to complete and obtain the return code using process.wait()
.
Catching Stdout In Realtime From Subprocess
To capture and process the standard output (stdout) of a subprocess in real-time in Python, you can use the subprocess.Popen
class along with a separate thread to continuously read and process the output. This allows you to capture stdout as it is generated by the subprocess. Here’s an example of how to do this:
import subprocess
import threading
# Define the command you want to run
command = ["ping", "example.com"]
# Create a Popen object to run the command
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True)
# Define a function to read and process stdout
def capture_stdout():
while True:
line = process.stdout.readline()
if not line:
break # No more output, exit the loop
print("stdout:", line.strip()) # Process and print the line
# Start a separate thread to capture stdout
stdout_thread = threading.Thread(target=capture_stdout)
stdout_thread.start()
# Wait for the process to complete and get the return code
return_code = process.wait()
stdout_thread.join() # Wait for the stdout thread to finish
# Print the final return code
print("Return Code:", return_code)
Code language: Python (python)
In this example:
- We use
subprocess.Popen
to start the subprocess with the specified command. We also capture both stdout and stderr for completeness. - We define a function
capture_stdout()
that continuously reads and processes the lines from stdout until there is no more output. You can modify this function to process the output as needed. - We start a separate thread,
stdout_thread
, to run thecapture_stdout()
function. This allows us to capture stdout in real-time while the main thread can continue to do other tasks. - After the subprocess completes, we wait for both the subprocess and the stdout thread to finish using
process.wait()
andstdout_thread.join()
.
By using a separate thread to capture stdout, you can process and display the output in real-time without blocking the main thread.
Read More;
- Python Subprocess Tutorial
- How to use the subprocess Popen.communicate() method
- Python Subprocess Output To File
- Python Subprocess Output To Variable
- Multiple Commands With SSH Using Python Subprocess
- Python Subprocess Run In Background
- Python Execute Shell Command And Get Output
- Face Recognition Based Attendance System Using Python
- Python Subprocess Pipe With Example
- Python Subprocess Interactive
- Python Subprocess AWK
- What is subprocess.wait() With Example