You are not logged in.
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
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
arch + gentoo + initng + python = enlisy
Offline
lol, thanks, I'll try to avoid him in my python threads
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