Python tarfile 进度
在 python 中将文件添加到 tar 存档时是否有任何库可以显示进度,或者可以扩展 tarfile 模块的功能来执行此操作?
在理想的情况下,我想展示 tar 创建的总体进度以及何时完成的预计时间。
对此的任何帮助将不胜感激。
Is there any library to show progress when adding files to a tar archive in python or alternativly would be be possible to extend the functionality of the tarfile module to do this?
In an ideal world I would like to show the overall progress of the tar creation as well as an ETA as to when it will be complete.
Any help on this would be really appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不幸的是,看起来没有一种简单的方法来获取逐字节数字。
您是否正在向此 tar 文件添加非常大的文件?如果没有,我将逐个文件更新进度,以便将文件添加到 tar 时,进度会根据每个文件的大小进行更新。
假设所有文件名都在变量
toadd
中,并且tarfile
是一个TarFile
对象。怎么样,Unfortunately it doesn't look like there is an easy way to get byte by byte numbers.
Are you adding really large files to this tar file? If not, I would update progress on a file-by-file basis so that as files are added to the tar, the progress is updated based on the size of each file.
Supposing that all your filenames are in the variable
toadd
andtarfile
is aTarFile
object. How about,查找或编写一个类似于包装提供进度报告的真实文件的文件,并将其传递给
Tarfile.addfile()
以便您可以知道已请求包含在存档中的字节数。如果Tarfile
尝试立即读取整个文件,您可能必须使用/实施限制。Find or write a file-like that wraps a real file which provides progress reporting, and pass it to
Tarfile.addfile()
so that you can know how many bytes have been requested for inclusion in the archive. You may have to use/implement throttling in caseTarfile
tries to read the whole file at once.我最近编写了一个提供进度回调的包装库。在 git hub 上查看它:
https://github.com/thomaspurchas/tarfile-Progress-记者
如果您需要帮助,请随时寻求帮助。
I have recently written a wrapper library that provides a progress callback. Have a look at it on git hub:
https://github.com/thomaspurchas/tarfile-Progress-Reporter
Feel free to ask for help if you need it.
似乎您可以使用
tarfile.add()
的filter
参数从
TarInfo
对象获取的所有信息都可供您使用。Seems like you can use the
filter
parameter oftarfile.add()
All information you can get from a
TarInfo
object is available to you.如何将文件添加到 tar 文件中?是通过 recursive=True 的“add”吗?您可以自己构建文件列表,然后逐一调用“添加”,显示进度。如果您从流/文件构建,那么看起来您可以包装该 fileobj 以查看读取状态并将其传递到 addfile 中。
看起来您根本不需要修改 tarfile.py 。
How are you adding files to the tar file? Is is through "add" with recursive=True? You could build the list of files yourself and call "add" one-by-one, showing the progress as you go. If you're building from a stream/file then it looks like you could wrap that fileobj to see the read status and pass that into addfile.
It does not look like you will need to modify tarfile.py at all.