I'm trying to read the last line from a command like 'apt-get download firefox'. Normally the output will be like
Get:1 http://archive.ubuntu.com/ubuntu/ utopic/main firefox amd64 32.0+build1-0ubuntu2 [34.9 MB]
2% [1 firefox 646 kB/34.9 MB 2%]
with the last line continuously updating (it is not writing a newline until it reaches 100%). My goal is now to read the progress in realtime. Here is my current example code:
#!/usr/bin/python3 -u
# coding=utf-8
import subprocess, sys
pipe = subprocess.Popen(['apt-get', 'download', 'firefox'], 0, stderr = subprocess.PIPE, stdout = subprocess.PIPE)
while True:
content = pipe.stdout.read(1).decode()
if content == '':
break
sys.stdout.write(content)
sys.stdout.flush()
pipe.wait()
I have disabled the output buffering for the subprocess call and also for binary output for the Python process (with the -u argument). But I'm only getting the first line but not the progress of the second line. Does somebody know how I can achieve this?
Get:1 http://..
line (stdout?) or is it enough if you get only the last (progress) line (stderr?)? – Blitherapt-get
writes only to stdout/stderr or can it write to the terminal directly? 2. Does it use the block-buffering if the stdout is a pipe? 3. Does it show the progress line if stderr is redirected to a pipe? – Blither