Created
March 29, 2015 21:56
-
-
Save SunnyRaj/2f72117556664552ffe9 to your computer and use it in GitHub Desktop.
Calculate the percentage completed while processing a input file using carriage return
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
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 "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