在 Python 中跟踪文件加载进度

发布于 2024-07-11 14:25:14 字数 84 浏览 10 评论 0原文

我使用的许多模块都会将整个文件导入内存或在处理文件时将文件的内容滴入其中。 我想知道是否有任何方法可以跟踪这种加载进度? 可能是一个需要回调的包装类?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云雾 2024-07-18 14:25:14

我会通过确定文件的大小,然后简单地将总数除以读取的字节数来实现这一点。 像这样:

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 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 

Edit: I know it isn't the best code, but I just wanted to show the concept.

最美的太阳 2024-07-18 14:25:14

如果您实际上是指“导入”(而不是“读取”),那么您可以覆盖导入模块定义。 您可以添加计时功能。

请参阅 imp 模块。

如果您的意思是“读取”,那么您可以使用自己的类似文件的包装器简单地包装 Python 文件。 文件不会公开太多方法。 您可以覆盖有趣的部分来获取计时数据。

>>> class MyFile(file):
...     def read(self,*args,**kw):
...         # start timing
...         result= super(MyFile,self).read(*args,**kw)
...         # finish timing
...         return result

If you actually mean "import" (not "read") then you can override the import module definitions. You can add timing capabilities.

See the imp module.

If you mean "read", then you can trivially wrap Python files with your own file-like wrapper. Files don't expose too many methods. You can override the interesting ones to get timing data.

>>> class MyFile(file):
...     def read(self,*args,**kw):
...         # start timing
...         result= super(MyFile,self).read(*args,**kw)
...         # finish timing
...         return result
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文