code_junkie

Tracking file load progress in Python

Nov 13th, 2011
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.65 KB | None | 0 0
  1. import os
  2.  
  3. def show_progress(file_name, chunk_size=1024):
  4. fh = open(file_name, "r")
  5. total_size = os.path.getsize(file_name)
  6. total_read = 0
  7. while True:
  8. chunk = fh.read(chunk_size)
  9. if not chunk:
  10. fh.close()
  11. break
  12. total_read += len(chunk)
  13. print "Progress: %s percent" % (total_read/total_size)
  14. yield chunk
  15.  
  16. for chunk in show_progress("my_file.txt"):
  17. # Process the chunk
  18. pass
  19.  
  20. >>> class MyFile(file):
  21. ... def read(self,*args,**kw):
  22. ... # start timing
  23. ... result= super(MyFile,self).read(*args,**kw)
  24. ... # finish timing
  25. ... return result
Add Comment
Please, Sign In to add comment