As part of a Python script, you are extracting a tar file. But you want to output the name of each file in the tar as it is being extracted (for logging, etc). How to do this?
Below is a code snippet that worked well for me:
1 2 3 4 5 6 7 8 9 10 |
import tarfile print "Extracting the contents of sample.tar.gz:" tar = tarfile.open("sample.tar.gz") for member_info in tar.getmembers(): print "- extracting: " + member_info.name tar.extract(member_info) tar.close() |
getmembers()
returns the members of the archive as a list of TarInfo objects.
You can then simply print the .name
of each member, before then extracting that file.
Therefore, if your extract failed for any reason, this allows your logs to show which file the extract failed on.