Skip to content

Instantly share code, notes, and snippets.

@kenchangh
Created May 11, 2014 09:49
Show Gist options
  • Save kenchangh/61bada7eb1fca376e8e9 to your computer and use it in GitHub Desktop.
Save kenchangh/61bada7eb1fca376e8e9 to your computer and use it in GitHub Desktop.
Python - Progress Bar
import sys
def progress_bar(list_size):
# list_size takes an integer as parameter
lst = range(list_size)
for index, item in enumerate(lst):
# List index starts from 0
# Round position / size of list to 1 decimal place
# 0.567777 => 0.57
# Multiplied by 100 to give you the percentage!
percent = int(round((index + 1) / len(lst), 2) * 100)
# Round number to 1 significant figure
# 56 => 60
# Divide by 10 to get number of times to print
times = int(round(percent, -1) / 10)
sys.stdout.write("\r{0}% {1}".format(percent,
'[' + '=' * times + ' ' * (10-times) + ']'))
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment