监控 ZIP 文件提取 Python
我需要解压缩 .ZIP 存档。我已经知道如何解压缩它,但它是一个巨大的文件,需要一些时间才能解压。如何打印提取完成的百分比?我想要这样的东西:
Extracting File
1% Complete
2% Complete
etc, etc
I need to unzip a .ZIP archive. I already know how to unzip it, but it is a huge file and takes some time to extract. How would I print the percentage complete for the extraction? I would like something like this:
Extracting File
1% Complete
2% Complete
etc, etc
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是一个您可以开始的示例,它没有经过优化:
为了使其更美观,在打印时执行以下操作:
here an example that you can start with, it's not optimized:
to make it more beautiful do this when printing:
您可以使用
tqdm()
监控每个文件的提取进度:You can just monitor the progress of each file being extracted with
tqdm()
:在 python 2.6 ZipFile 对象中有一个 open 方法,可以打开一个zip 中的命名文件作为文件对象,您可以起诉它以分块读取数据
编辑:要获取总大小,您可以执行类似的操作
现在您可以打印总进度和每个文件的进度,例如假设您只有 1 zip 中的大文件,其他方法(例如仅计算文件大小并提取)根本不会提供任何进展。
In python 2.6 ZipFile object has a open method which can open a named file in zip as a file object, you can sue that to read data in chunks
Edit: To get the total size you can do something like this
So now you can print the total progress and progress per file, e.g. suppose you have only 1 big file in zip, other methods(e.g. just counting file sizes and extract) will not give any progress at all.
ZipFile.getinfolist()
将从 zip 文件的内容生成许多ZipInfo
对象。从那里,您可以合计存档中所有文件的字节数,然后计算到目前为止已提取的文件数,也可以按文件总数进行计算。ZipFile.getinfolist()
will generate a number ofZipInfo
objects from the contents of the zip file. From there you can either total up the number of bytes of all the files in the archive and then count up how many you've extracted thus far, or you can go by the number of files total.我不相信您可以跟踪提取单个文件的进度。 zipfile 提取函数 没有进度回调。
I don't believe you can track the progress of extracting a single file. The zipfile extract function has no callback for progress.