How do I list contents of a gzip file without extr

2019-03-02 05:01发布

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?

1条回答
贪生不怕死
2楼-- · 2019-03-02 05:22

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

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

print([(member.name, member.mtime) for member in tarf.getmembers()])
查看更多
登录 后发表回答