When invoking a subprocess using subprocess.Popen, we can simultaneously store its output for logging purposes and display it live on the terminal.
Option 1: Using Iterators
import subprocess import sys with open("test.log", "wb") as f: process = subprocess.Popen(your_command, stdout=subprocess.PIPE) for c in iter(lambda: process.stdout.read(1), b""): sys.stdout.buffer.write(c) f.buffer.write(c)
Option 2: Using a Reader and a Writer
import io import time import subprocess import sys filename = "test.log" with io.open(filename, "wb") as writer, io.open(filename, "rb", 1) as reader: process = subprocess.Popen(command, stdout=writer) while process.poll() is None: sys.stdout.write(reader.read()) time.sleep(0.5) # Read the remaining sys.stdout.write(reader.read())
Option 3: Custom Solution
ret_val = subprocess.Popen(run_command, stdout=log_file, stderr=subprocess.PIPE, shell=True) while not ret_val.poll(): log_file.flush()
In another terminal, run:
tail -f log.txt
With these methods, you can capture subprocess output while also displaying it live, ensuring both logging and progress monitoring.
The above is the detailed content of How Can I Simultaneously Capture and Display Live Output from a Subprocess in Python?. For more information, please follow other related articles on the PHP Chinese website!