Skip to content

Instantly share code, notes, and snippets.

@matthewpoer
Created April 23, 2021 13:57
Show Gist options
  • Save matthewpoer/65eff844150cc8b51537010ec12cad64 to your computer and use it in GitHub Desktop.
Save matthewpoer/65eff844150cc8b51537010ec12cad64 to your computer and use it in GitHub Desktop.
Progress counting tracking python
import time
some_list = [1,2,3,4,5,6]
counter = 0
for file in some_list:
counter += 1
percent = round((counter/len(some_list))*100, 2)
message = "==== Progress: " + str(percent) + "% ===="
if percent < 100:
# this line ending and buffer-flush allow us to re-write over this line again
print(message, end='\r', flush=True)
else:
# once we reach 100%, we won't want to rewrite the line again
print(message)
time.sleep(1.5)
# prints on its own line
print("done :)")

While in-progress

> python test-counting.py 
==== Progress: 33.33% ====
> python test-counting.py 
==== Progress: 50.0% =====

Final resting output:

> python test-counting.py 
==== Progress: 100.0% ====
done :)
@matthewpoer
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment