import sys from math import ceil from time import sleep def progress(total, length=20, bar='=', empty=' '): point = total/100 increment = total/length for i in range(total+1): # add one as counting starts at 0 sys.stdout.write('\r') fill = int(i/increment) # use ceil to ensure that 100% is reached, just in case of low precision sys.stdout.write("[%s%s] %3d%%" % (bar*fill, empty*(length-fill), ceil(i/point))) sys.stdout.flush() sleep(0.1) print() def progress_simple(total, bar='='): point = total/100 increment = total/20 for i in range(total+1): # add one as counting starts at 0 sys.stdout.write('\r') sys.stdout.write("[%-20s] %3d%%" % (bar*int(i/increment), ceil(i/point))) sys.stdout.flush() sleep(0.1) print() try: total = int(sys.argv[1]) except IndexError: total = 25 except ValueError: exit("The given argument is not a valid number!") except: exit("Unknown error") progress_simple(total) progress(total, 40, '#', '-')