开发者

python: walking tar archive with gzip files

开发者 https://www.devze.com 2023-02-07 13:42 出处:网络
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.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号