Accessing Real-Time Program Output with Subprocess
Question:
How can we obtain real-time program output using subprocess in Python? Specifically, how can we display the progress of a command-line program without buffering the output?
Answer:
To get real-time output from a program executed with subprocess, we can use the following technique:
import subprocess p = subprocess.Popen('svnadmin verify /var/svn/repos/config', stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, bufsize=0) # Set bufsize to 0 for no buffering while True: line = p.stdout.readline() if not line: break print(line.replace('\n', ''))
Explanation:
Note:
The above is the detailed content of How to Display Real-time Program Output with Python's `subprocess`?. For more information, please follow other related articles on the PHP Chinese website!