When 7zip runs from the command line, it will print a progress bar using a series of '%' symbols.
I'd like to both capture and print this progress bar when executing 7zip from within Python. How do I do this?
The Python code I'm currently using:
from subprocess import Popen, PIPE
pipe = Popen('7za.exe a -tgzip "e:\\backup\\sch Testerr 2012 06 23 17-27.gzip" "E:/archiv"' , stdout=PIPE)
text = pipe.communicate()[0]
print text
From
communicate
(emphasis mine):Consider using
poll
instead:What you want is sys.stdout.flush().
However, you may need to execute flush on a separate thread since the main thread is probably blocked until the underlying process in Popen is complete.
Edit: Using Brian's answer to help (and to avoid multi-threading), I'd envisage a solution like this:
I found 7za suppress progress output when stdout is redirected.
So, I wrote a patch.
'sopg' option enables progress output even when stdout is redirected or piped. https://github.com/photom/p7zip/commit/2baacd6c354fbde19ebc83d185e73f6d9bd62517
$ 7za x -sopg ..7z