Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (47.6k points)

How do I use a progress bar when my script is doing some task that is likely to take time?

For example, a function which takes some time to complete and returns True when done. How can I display a progress bar during the time the function is being executed?

Note that I need this to be in real-time, so I can't figure out what to do about it. Do I need a thread for this? I have no idea.

Right now I am not printing anything while the function is being executed, however, a progress bar would be nice. Also, I am more interested in how this can be done from a code point of view.

1 Answer

0 votes
by (106k points)

You can use the below-mentioned code to implement Python progress bar:-

import time 

import sys 

toolbar_width = 40 

sys.stdout.write("[%s]" % (" " * toolbar_width)) 

sys.stdout.flush() 

sys.stdout.write("\b" * (toolbar_width+1)) 

for i in xrange(toolbar_width): 

time.sleep(0.1) 

sys.stdout.write("-") 

sys.stdout.flush() 

sys.stdout.write("]\n") 

Related questions

0 votes
1 answer
asked Jul 13, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
asked Apr 29, 2021 in Java by sheela_singh (9.5k points)
0 votes
1 answer

Browse Categories

...