4

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
hythlodayr
  • 2,377
  • 15
  • 23
  • Your question is completely unclear. What "percent"? – BrenBarn Jun 30 '12 at 00:54
  • is it clear now ? i mean a command progress percentage , like creating an archive or exract it , sory for bad english – Sepehr Mohammad Pour Jun 30 '12 at 01:23
  • No it's not clear. You will need to give an example of the command output. No one can say how to get the percentage if you don't show what it looks like and what kind of text it's surrounded by. – BrenBarn Jun 30 '12 at 01:25
  • problem is when i run 7z command line with Popen , and it just return an final output when its done ! , cant get any output when command is running – Sepehr Mohammad Pour Jun 30 '12 at 02:08
  • `tar cf - directory | pv --size $(du -sb directory | cut -f1) | 7z a -si directory.7z` shows progress bar on *nix. – jfs Jun 30 '12 at 02:14
  • related [Python subprocess get children's output to file and terminal?](http://stackoverflow.com/q/4984428/4279) – jfs Jul 02 '12 at 06:16
  • I have a same problem. I don't know how, but, 7za seems to disable progress output when redirect. Is there anyone who solved this question? https://sourceforge.net/p/sevenzip/discussion/45798/thread/deca5a64/ – tabata Dec 02 '16 at 06:10

3 Answers3

1

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:

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)

# Assuming Python thread continues after POpen invoke (but before 7za is finished)
while (not pipe.poll()):
    sys.stdout.flush()
    time.sleep(1)
hythlodayr
  • 2,377
  • 15
  • 23
  • Flush forces buffered output to standard out. See http://stackoverflow.com/questions/10019456/usage-of-sys-stdout-flush-method. – hythlodayr Jul 02 '12 at 14:51
1

From communicate (emphasis mine):

Read data from stdout and stderr, until end-of-file is reached. Wait for process to terminate.

Consider using poll instead:

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)
while True: # Feeling fancy?  add a timeout condition here...
    if pipe.poll():
        break
    # consider reading from stdin and printing as your needs indicate
Brian Cain
  • 14,403
  • 3
  • 50
  • 88
  • when i use communicate in a thread python stop in that line ! and dont return any output – Sepehr Mohammad Pour Jul 02 '12 at 14:25
  • @SepehrMohammadPour, Omit the call to `communicate`, it's not necessary. The `pipe = Popen('7za.exe ...` part has already invoked `7za`, `communicate` will wait until `7za` completes, but `poll` will indicate whether or not it has completed. – Brian Cain Jul 02 '12 at 16:14
1

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

tabata
  • 449
  • 6
  • 17
  • author's comment is following. https://sourceforge.net/p/sevenzip/discussion/45797/thread/d10225f7/#3d6e/073b/2c76/26a8/9bd6 – tabata Dec 02 '16 at 12:38
  • Is this flag still active? I'm on Windows 10, 7zip 19.00, trying `-sopg` and get `Too long switch: -sopg`. I understood from the docu that `-bsp1` should work, but it does nothing for me :/ ( Python script does not grab the progress) – Jay Aug 01 '20 at 17:08
  • 1
    This patch above is not merged into official 7-zip. If you want to use this, you need to merge and build for yourself. – tabata Aug 04 '20 at 08:55
  • Do you know of anything else that can be done on Windows? – Jay Aug 04 '20 at 10:46
  • https://github.com/photom/7z/releases/tag/19.00a If you do not mind using binary built on my appveyor, patched 7za.exe exists on the link above. I do not know how to solve this problem by useing official binary as is. – tabata Aug 06 '20 at 10:26