Skip to content

Instantly share code, notes, and snippets.

@kavinyao
Created July 25, 2013 06:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kavinyao/6077326 to your computer and use it in GitHub Desktop.
Save kavinyao/6077326 to your computer and use it in GitHub Desktop.
import sys
import math
import time
def progress_bar(progress, col_width=80):
"""
Text progress bar in command line.
Pre-condition: the cursor is at a new line.
Post-condition: the cursor is at the end of the same line.
"""
progress_width = col_width - 10
finished = int(progress * progress_width)
progress_str = '#'*finished + '-'*(progress_width-finished)
sys.stdout.write('\r[%s](%.1f%%)' % (progress_str, 100*progress))
sys.stdout.flush()
limit = 42
print
for i in range(limit):
progress = 1.0*(i+1)/limit
progress_bar(progress)
time.sleep(0.1)
print
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment