I wrote this function to take the subprocess
output line-by-line, which then yield
s each line and sends it to the user.
def invoke_process_popen_poll_live(command, shellType=False, stdoutType=subprocess.PIPE):
"""runs subprocess with Popen/poll so that live stdout is shown"""
try:
process = subprocess.Popen(command, shell=shellType, stdout=stdoutType, universal_newlines=True)
except:
print("ERROR {} while running {}".format(sys.exc_info()[1], command))
return None
while True:
output = process.stdout.readline()
yield output.strip()
if process.poll() is not None:
break
if output:
output.strip()
rc = process.poll()
return rc
The output is the youtube-dl
download stats which is like this:
[youtube] knge_1uWbro: Downloading webpage
[download] Destination: ./DLVids/Kitty_says_Uh-Oh_very_clearly-knge_1uWbro-720.f398.mp4
[download] 0.1% of 963.52KiB at 3.82KiB/s ETA 04:11
[download] 0.3% of 963.52KiB at 11.46KiB/s ETA 01:23
[download] 0.7% of 963.52KiB at 26.61KiB/s ETA 00:35\n
.
.
.
Is there any way to take each line's output like this:
0.1% of 963.52KiB at 3.82KiB/s
0.3% of 963.52KiB at 11.46KiB/s
0.7% of 963.52KiB at 26.61KiB/s