Python tarfile 进度

发布于 2024-10-12 08:32:57 字数 138 浏览 6 评论 0原文

在 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 技术交流群。

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

发布评论

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

评论(5

中二柚 2024-10-19 08:32:57

不幸的是,看起来没有一种简单的方法来获取逐字节数字。

您是否正在向此 tar 文件添加非常大的文件?如果没有,我将逐个文件更新进度,以便将文件添加到 tar 时,进度会根据每个文件的大小进行更新。

假设所有文件名都在变量 toadd 中,并且 tarfile 是一个 TarFile 对象。怎么样,

from itertools import imap
from operator import attrgetter
# you may want to change this depending on how you want to update the
# file info for your tarobjs
tarobjs = imap(tarfile.getattrinfo, toadd)
total = sum(imap(attrgetter('size'), tarobjs))
complete = 0.0
for tarobj in tarobjs:
    sys.stdout.write("\rPercent Complete: {0:2.0d}%".format(complete))
    tarfile.add(tarobj)
    complete += tarobj.size / total * 100
sys.stdout.write("\rPercent Complete: {0:2.0d}%\n".format(complete))
sys.stdout.write("Job Done!")

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 and tarfile is a TarFile object. How about,

from itertools import imap
from operator import attrgetter
# you may want to change this depending on how you want to update the
# file info for your tarobjs
tarobjs = imap(tarfile.getattrinfo, toadd)
total = sum(imap(attrgetter('size'), tarobjs))
complete = 0.0
for tarobj in tarobjs:
    sys.stdout.write("\rPercent Complete: {0:2.0d}%".format(complete))
    tarfile.add(tarobj)
    complete += tarobj.size / total * 100
sys.stdout.write("\rPercent Complete: {0:2.0d}%\n".format(complete))
sys.stdout.write("Job Done!")
各空 2024-10-19 08:32:57

查找或编写一个类似于包装提供进度报告的真实文件的文件,并将其传递给 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 case Tarfile tries to read the whole file at once.

月亮是我掰弯的 2024-10-19 08:32:57

我最近编写了一个提供进度回调的包装库。在 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.

年华零落成诗 2024-10-19 08:32:57

似乎您可以使用 tarfile.add()filter 参数

with tarfile.open(<tarball path>, 'w') as tarball:
   tarball.add(<some file>, filter = my_filter)

def my_filter(tarinfo):
   #increment some count
   #add tarinfo.size to some byte counter
   return tarinfo

TarInfo 对象获取的所有信息都可供您使用。

Seems like you can use the filter parameter of tarfile.add()

with tarfile.open(<tarball path>, 'w') as tarball:
   tarball.add(<some file>, filter = my_filter)

def my_filter(tarinfo):
   #increment some count
   #add tarinfo.size to some byte counter
   return tarinfo

All information you can get from a TarInfo object is available to you.

酒与心事 2024-10-19 08:32:57

如何将文件添加到 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文