在Python中跟踪文件加载进度 [英] Tracking file load progress in Python
本文介绍了在Python中跟踪文件加载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用许多模块将整个文件导入内存或在文件处理过程中滴入文件的内容.我想知道是否有任何方法可以跟踪这种加载进度?可能是包装类需要回调吗?
A lot of modules I use import entire files into memory or trickle a file's contents in while they process it. I'm wondering if there's any way to track this sort of loading progress? Possibly a wrapper class that takes a callback?
推荐答案
我将通过确定文件的大小,然后将总数除以读取的字节数来做到这一点.像这样:
I would do by this by determining the size of the file, and then simply dividing the total by the number of bytes read. Like this:
import os
def show_progress(file_name, chunk_size=1024):
fh = open(file_name, "r")
total_size = os.path.getsize(file_name)
total_read = 0
while True:
chunk = fh.read(chunk_size)
if not chunk:
fh.close()
break
total_read += len(chunk)
print "Progress: %s percent" % (total_read/total_size)
yield chunk
for chunk in show_progress("my_file.txt"):
# Process the chunk
pass
我知道这不是最好的代码,但我只是想展示这个概念.
I know it isn't the best code, but I just wanted to show the concept.
这篇关于在Python中跟踪文件加载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文