Python to print out status bar and percentage

Python to print out status bar and percentage

The r character (carriage return) resets the cursor to the beginning of the line and allows you to write over what was previously on the line.

from time import sleep
import sys

for i in range(21):
    # the exact output youre looking for:
    sys.stdout.write([%-20s] %d%% % (=*i, 5*i))

Im not 100% sure if this is completely portable across all systems, but it works on Linux and OSX at the least.

Theres a Python module that you can get from PyPI called progressbar that implements such functionality. If you dont mind adding a dependency, its a good solution. Otherwise, go with one of the other answers.

A simple example of how to use it:

import progressbar
from time import sleep
bar = progressbar.ProgressBar(maxval=20, 
    widgets=[progressbar.Bar(=, [, ]),  , progressbar.Percentage()])
for i in xrange(20):

To install it, you can use easy_install progressbar, or pip install progressbar if you prefer pip.

Python to print out status bar and percentage

I found useful library tqdm (, previously: It automatically estimates time of completion and can be used as iterator.


import tqdm
import time

for i in tqdm.tqdm(range(1000)):
    # or other long operations

Results in:

|####------| 450/1000  45% [elapsed: 00:04 left: 00:05, 99.15 iters/sec]

tqdm can wrap any iterable.

Leave a Reply

Your email address will not be published. Required fields are marked *