JJ Adams
JJ Adams

Reputation: 511

Reading and printing progress bar that is being updated on same line while it is still updating

So I'm using subprocess.Popen in order to load new OS onto multiple devices.

my script:

done = None
command = 'my loading command'
proc = subprocess.Popen(command, stdout=subprocess.PIPE)

while proc.poll() is None:
    line = proc.stdout.readline()
    print line.strip()
    if "Done" in line:
        done = True

So the loading process should start off by detecting the device plugged into the system and then the loader/progress bar starts:

Connecting to Device:
Connected

[                  Writing   ] [               0                 ]
[                  Writing   ] [               1                 ]
[                  Writing   ] [               2                 ]
.
.
.
.
[                  Writing   ] [############## 100 ##############]


Done.

Now to be sure that the loading is completely done I use Popen with stdout=PIPE in order to check when the "Done." string is printed to stdout and then I print stdout onto the cmd window. Now my issue is that since proc.stdout.readline() is reading every line at a time, it prints the 1st 2 lines about detecting and connecting to device and then nothing for 10 min then it prints this line:

[                  Writing   ] [############## 100 ##############]

So my output on the cmd window looks like this:

Connecting to Device:
Connected

[                  Writing   ] [############## 100 ##############]

Done.

So it doesn't start from [Wrtiting 0] and then [Writing 1].... till it gets to [100]. this is because the loader is being updated on the same line so proc.stdout.readline() waits till the loading line is complete and then prints it out which pretty much defies the purpose of having the progress bar to show the progress made every coupe of seconds.

Can anyone help me solve this? I tried printing to both a file and the cmd window at same time to check for the "Done." string but no luck as it only prints '0' to the txt file.

Upvotes: 2

Views: 1834

Answers (2)

kerlyn
kerlyn

Reputation: 368

The issue is that a progress bar line ends with '\r' and not '\n'. If you change the code to the following it should work:

done = None
command = 'my loading command'
proc = subprocess.Popen(command, stdout=subprocess.PIPE, universal_newlines=True)

while proc.poll() is None:
    line = proc.stdout.readline()
    print line.strip()
    if "Done" in line:
        done = True

Upvotes: 0

JJ Adams
JJ Adams

Reputation: 511

alright So i found the answer t solve this problem:

In order to be able to check for the done string and print the progress bar as it progresses to 100% i use the same code except I have to specify the number of characters in readline(). So I did the loading and made it print it to a file and then copied the last line where the progress bar was full, entered it into a print len(str), got the length of the string and then added it as argument to the readline() command:

done = None
command = 'my loading command'
proc = subprocess.Popen(command, stdout=subprocess.PIPE)

while proc.poll() is None:
    line = proc.stdout.readline(77)
    print line.strip()
    if "Done" in line:
        done = True

Upvotes: 1

Related Questions