Real-time output of the subprocess .popen (), not line by line

I am currently rewriting a small wrapper program in python that I once wrote in C ++. It extracts files from a file and puts them in a different format.

In C ++, the output from the system commands that I need to run was "in real time", i.e. in the status bar and percentage indicator of some commands displayed in real time. With python, I get every "percentage" dumped on the screen individually (because I read it in turn). Here is an example: Here is what the status bar looks like in the python version (this extends to 100). In C ++, it updates itself.

| (02/100)\rImporting AVC-H264: | | (03/100)\rImporting AVC-H264: | | (04/100)\rImporting AVC-H264: |= 

Here is the corresponding python code:

 p = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE) for line in iter(p.stdout.readline, ""): print line, 

Any ideas on how I can make this look like C ++?

+4
source share
3 answers

Maybe two things ...

It is likely that readline changes some things from the results of your program. I believe that \r is a carriage return and tells the terminal to go back to the beginning and then the program can print on top of the text it just prints. Readline most likely removes this.

The first thing to try

 p = subprocess.Popen(args, stdout=subprocess.PIPE, \ stderr=subprocess.PIPE, \ universal_newlines=True) for line in iter(p.stdout.readline, ""): sys.stdout.write('\r'+line[:-1]) sys.stdout.flush() 

You need to make a flash because stdout buffers until it gets \n and of course you don't write.

+6
source

On Windows, you can output the reverse character (ASCII code 8). Example (prints the current iteration using only digits with one digit):

 >>> import time >>> import sys >>> for i in xrange(10): ... sys.stdout.write(str(i)) ... time.sleep(.5) ... sys.stdout.write(chr(8)) ... 

You will need to keep track of the number of characters in the current line ... I am sure there should be a better way.

On Linux, it looks like you can write a carriage return to the reset cursor position. See Delete the current printed cantilever line and Result of execution in place in the terminal or console .

+1
source

Sorry, but I didnโ€™t understand the real-time part very well, but maybe I can help with the โ€œupdate it myselfโ€ part, try the following:

 for line in iter(p.stdout.readline, ""): print line + '\r', 
0
source

Source: https://habr.com/ru/post/1334584/


All Articles