Created
          October 24, 2017 03:03 
        
      - 
      
 - 
        
Save kdoroschak/499417034ff0b5f0533ed994b1e8f3a3 to your computer and use it in GitHub Desktop.  
    Python 2.7 for loop progress bar
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | ''' Python 2.7 progress bar | |
| Code mostly taken from: | |
| https://stackoverflow.com/questions/3173320/text-progress-bar-in-the-console | |
| I usually use this for simple for loops with long iterations to make sure it's | |
| actually progressing without polluting stdout. | |
| ''' | |
| import sys | |
| n_iterations = 100 | |
| bar_len = 60 | |
| # Assuming the loop always starts from 0 | |
| for iterations_done in range(0, n_iterations): | |
| filled_len = int(round(bar_len * (iterations_done + 1) / | |
| float(n_iterations))) | |
| percents = round(100.0 * iterations_done / float(n_iterations), 1) | |
| bar = '=' * filled_len + '-' * (bar_len - filled_len) | |
| sys.stdout.write('[%s] %s%s, %d of %d\r' % (bar, percents, '%', | |
| iterations_done + 1, | |
| n_iterations)) | |
| sys.stdout.flush() | |
| # Do stuff here | |
| filled_len = bar_len | |
| percents = 100. | |
| bar = '=' * filled_len + '-' * (bar_len - filled_len) | |
| sys.stdout.write('[%s] %s%s \r' % (bar, percents, '%')) | |
| sys.stdout.flush() | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment