Python tarfile 进度输出?
我正在使用以下代码来提取 tar 文件:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
但是,我想以当前正在提取哪些文件的形式密切关注进度。我该怎么做?
额外奖励积分:是否也可以创建提取过程的百分比?我想用 tkinter 来更新进度条。谢谢!
I'm using the following code to extract a tar file:
import tarfile
tar = tarfile.open("sample.tar.gz")
tar.extractall()
tar.close()
However, I'd like to keep tabs on the progress in the form of which files are being extracted at the moment. How can I do this?
EXTRA BONUS POINTS: is it possible to create a percentage of the extraction process as well? I'd like to use that for tkinter to update a progress bar. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
文件进度和全局进度:
Both file-progress and global progress:
您可以在
extractall()
中指定members
参数为
TarInfo
对象,查看所有可用的函数和属性< a href="http://docs.python.org/2/library/tarfile.html#tarinfo-objects" rel="noreferrer">此处You can specify the
members
parameter inextractall()
member
areTarInfo
objects, see all available functions and properties here您只需使用
tqdm()
并打印文件数量的进度提取:You can just use
tqdm()
and print the progress of the number of files being extracted:您可以使用
extract
代替extractall
- 您可以在提取成员名称时打印它们。要获取成员列表,您可以使用 getmembers。可以在此处找到文本进度条库:
Tkinter 片段:
You could use
extract
instead ofextractall
- you would be able to print the member names as they are being extracted. To get a list of members, you could use getmembers.A textual progressbar library can be found here:
Tkinter snippet:
这里有一个很酷的解决方案,它可以覆盖 tarfile 模块作为直接替换,并允许您指定要更新的回调。
https://github.com/thomaspurchas/tarfile-Progress-Reporter/
根据评论更新
There's a cool solution here that overrides the tarfile module as a drop-in replacement and lets you specify a callback to update.
https://github.com/thomaspurchas/tarfile-Progress-Reporter/
updated based on comment
要查看当前正在提取哪个文件,以下内容对我有用:
To see which file is currently being extracted, the following worked for me:
这就是我使用的,无需猴子修补或需要条目数。
This is what I use, without monkey patching or needing the number of entries.