5

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?

bsplosion
  • 2,641
  • 27
  • 38
Denys S.
  • 6,358
  • 12
  • 43
  • 65
  • Does this answer your question? [Extracting gzip folder of files in Python](https://stackoverflow.com/questions/33680023/extracting-gzip-folder-of-files-in-python) – bsplosion Mar 05 '20 at 17:33

1 Answers1

7

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()])
mgbelisle
  • 571
  • 5
  • 8