python sleep progress bar issue not working fine

python sleep progress bar issue not working fine

Problem Description:

I referred to one post of stackoverflow and created a progress bar for waiting but it does not work as expected. Can someone please help me out

def progress(count, total, suffix='waiting'):
    bar_len = 60

    filled_len = int(round(bar_len * count / float(total)))

    percents = round(100.0 * count / float(total), 1)
    bar = '=' * filled_len + '-' * (bar_len - filled_len)

    sys.stdout.write('[%s] %s%s ...%sr' % (bar, percents, '%', suffix))
    sys.stdout.flush()

def sleeper(seconds):
    for i in range(seconds):
        progress(i+1, seconds)
        time.sleep(1)
        seconds -= 1

When i call sleeper(10) after 5 seconds it messes up.

Solution – 1

There is no need to decrement seconds for each iteration, which decreases the total of the progress bar and skews the bar ratio:

Remove this line:

seconds -= 1
Rate this post
We use cookies in order to give you the best possible experience on our website. By continuing to use this site, you agree to our use of cookies.
Accept
Reject