How do I list contents of a gzip file without extracting it in python?

拜拜、爱过 提交于 2019-12-31 04:39:06

问题


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:


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()])


来源:https://stackoverflow.com/questions/19666983/how-do-i-list-contents-of-a-gzip-file-without-extracting-it-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!