Skip to content

Instantly share code, notes, and snippets.

@SunnyRaj
Created March 29, 2015 21:56
Show Gist options
  • Save SunnyRaj/2f72117556664552ffe9 to your computer and use it in GitHub Desktop.
Save SunnyRaj/2f72117556664552ffe9 to your computer and use it in GitHub Desktop.
Calculate the percentage completed while processing a input file using carriage return
import sys
import os
from time import sleep
path = "text"
file_size = os.stat(path).st_size
f = open(path, 'r', 1)
bytes_read = 0
for line in f:
bytes_read += len(line)
percent = bytes_read * 100 / file_size
sys.stdout.write("\rpercentage: " + str(percent) + "%")
sys.stdout.flush()
sleep(1)
print
print "bytes_read:", bytes_read
print "file_size:", file_size
# Ref:https://evilzone.org/scripting-languages/%28python%29-problem-with-percentage-completed-progress/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment