You are not logged in.

#1 2005-03-21 17:52:31

cmp
Member
Registered: 2005-01-03
Posts: 350

python: using tarfile with a temporary file

Hi, I'm really new to python (started this afternoon), but I allready run into a problem.
I try to download a .tar.gz file from a website. The downloading via urllib2 works perfectly.
As tarfile.open can't be used with urllib2 file objects directly (at least it didn't work the first time I tried), I'm saving the content of the .tar.gz file into a named temporary file via copyfileobj. But when I try to open it with tarfile.open it does not work: after some files from the archive it raises an exception which says that a crc check failed.
When I save the same file into a normal file (opened via open) it works flawlessly, but I would appreciate it, if I can use a temporary file.
I hope there is somebody who can help me.

    def readDB(self, db):
        tar = tarfile.open(db, 'r')
        for tarinfo in tar:
            print tarinfo.name, "is", tarinfo.size, "bytes in size and is",
            if tarinfo.isreg():
                print "a regular file."
            elif tarinfo.isdir():
                print "a directory."
            else:
                print "something else."
        tar.close()

works:

        file = urllib2.urlopen(url)
        f = open("out.tar.gz", "w")
        copyfileobj(file, f)
        file.close()
        f.close()
        readDB("out.tar.gz")

does not work

        file = urllib2.urlopen(url)
        tmp = NamedTemporaryFile( "w")
        copyfileobj(file, tmp)
        file.close()
        readDB(tmp.name)
        tmp.close()

Offline

#2 2005-03-21 22:57:35

xerxes2
Member
From: Malmoe, Sweden
Registered: 2004-04-23
Posts: 1,249
Website

Re: python: using tarfile with a temporary file

i think you get it wrong when you are running a loop on "tar" ,

try this:

for i in tar.getnames():

or if you want tarinfoobjects:

for i in tar.getmembers():

on tarinfo objects you can use the "name" attribute,

good luck and look out for cactus, he's an evil Ruby zealot  big_smile


arch + gentoo + initng + python = enlisy

Offline

#3 2005-03-22 10:50:51

cmp
Member
Registered: 2005-01-03
Posts: 350

Re: python: using tarfile with a temporary file

lol, thanks, I'll try to avoid him in my python threads wink

the problem was not connected to loop, but to the temp file, I removed the section with NamedTemporaryFile and used mstemp instead, I think the file got deleted before I was finished with reading.
But python rocks.

Offline

Board footer

Powered by FluxBB