Skip to content

Instantly share code, notes, and snippets.

@titangene
Created October 30, 2019 17:25
Show Gist options
  • Save titangene/cf58144002d1469b38f577c7056e21af to your computer and use it in GitHub Desktop.
Save titangene/cf58144002d1469b38f577c7056e21af to your computer and use it in GitHub Desktop.
Terminal progress bar with python
import time
import math
import os
rows, columns = os.popen('stty size', 'r').read().split()
# `[]` (2) + precent (5)
terminal_progress_fill = int(columns) - 7
current_progress = 0
while current_progress <= 100:
per = terminal_progress_fill / 100
current = math.floor(per * current_progress)
completed_progress_line = '=' * current
unfinished_progress_line = ' ' * (terminal_progress_fill - current)
current_progress_line = '[{}{}]'.format(completed_progress_line, unfinished_progress_line)
current_progress_precent = '{}%'.format('%3d' % current_progress)
current_progress_str = '{} {}'.format(current_progress_line, current_progress_precent)
print(current_progress_str, end="\r")
current_progress += 1
time.sleep(0.01)
print('finish')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment