Can someone point out any errors I might have included in my code. I wanted to keep the code as simple as possible but it’s failing with a memory error. Some of the compressed files do extract fine though…
import zipfile
import from os.path isdir, join, normpath, split
print "Unzipping data"
z = zipfile.ZipFile("C:\\Incoming\\MyZipFile.zip", 'r')
print z.namelist()
for each in z.namelist():
if not each.endswith('/'):
root, name = split(each)
print name
file(join("C:\\Incoming\\", name), 'wb').write(z.read(each))
z.close()
The actual error message generated is as follows:
Traceback (most recent call last):
File "C:\\Scripts\\Zip_import_test.py", line 30, in <module>
file(join("C:\\Incoming\\", name), 'wb').write(zip.read(each))
File "C:\\Python25\lib\zipfile.py, line 501, in read
bytes = dc.decompress(bytes)
Memory Error
Thanks for any suggestions.
Frank Ogiamien
Don’t call it
zip! You’re masking the builtin.Also, is
zip.close()really inside yourforloop? It shouldn’t be.You should use the
extractmethod of the ZipFile object, so you don’t need to read the whole file into memory.Instead of
do
Edit: This was added in 2.6, as was
extractallif you just want to extract the whole thing to a directory.If you can’t upgrade, the code in How to simulate ZipFile.open in Python 2.5? (in the question, not an answer) will let you use parse the zipfile and extract the data using zlib without reading it in to memory.