I have some .tar files (ungzipped). Each of them has some .gz files.
I need to walk through .tar file and get ungzipped content of all other files.
so I wrote:
#!/usr/bin/python2.5 -u
import tarfile
import zlib
ar 开发者_Go百科= tarfile.open('20101231.tar', 'r')
for item in ar:
if item.name[-3:] == ".gz":
print zlib.decompress(ar.extractfile(item).read())
f.close()
but it doesn't work! Error: "zlib.error: Error -3 while decompressing data: incorrect header check"
but I can do 'tar xvf 20101231.tar && gzip -d 20101231/some_file.gz' and all works perfectly! But I can't do it from python
Try tarfile.open('20101231.tar', 'r:')
to explicitly disable compression.
try this:
this_tar = tarfile.open(filepath)
for file in this_tar.getnames():
...do stuff...
returns each file in the TAR.
精彩评论