如何列出tar文件的内容而不在python中解压缩? [英] How do I list contents of a tar file without extracting it in python?

查看:104
本文介绍了如何列出tar文件的内容而不在python中解压缩?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的* .tar.gz文件,我想查看其中包含的文件列表而不提取内容(最好是每个文件使用mtimes)。我该如何在python中实现?

I've got a huge *.tar.gz file and I want to see the list of files contained in it without extracting the contents (preferably with mtimes per file). How can I achieve that in python?

推荐答案

您可以使用TarFile.getnames()这样:

You can use TarFile.getnames() like this:

#!/usr/bin/env python3

import tarfile
tarf = tarfile.open('foo.tar.gz', 'r:gz')
print(tarf.getnames())

http://docs.python.org/3.3/library /tarfile.html#tarfile.TarFile.getnames

如果需要mtime值,则可以使用getmembers()。

And if you want mtime values you can use getmembers().

print([(member.name, member.mtime) for member in tarf.getmembers()])

这篇关于如何列出tar文件的内容而不在python中解压缩?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆