Question

So I made a makeshift progress bar. Here's a mock version:

import sys, time

def Round(n):
    return str("{0:.2f}".format(n))

def updateProgressBar(label, percentage):
    width = 20
    done = width * int(percentage)/100
    sys.stdout.write('\r' + label + '\t[' + done * '*' + (width-done) * ' ' + ']\t' + Round(percentage) + '%')

n = 23
for i in range(n+1):
    time.sleep(0.25)
    updateProgressBar('Testing', 100*float(i)/n)

It's working just fine when ran from cmd, growing the % and incrementing the bar length. But when I run it through cygwin, nothing is displayed till the bar is at 100%, at which point it appears entirely, which defies the point. Any pointers please?

Was it helpful?

Solution

Figured it out. Adding sys.stdout.flush() did the trick. I don't fully understand why though, and would be curious to hear if someone can explain.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top