开发者

Zip code not working - memory error

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

Th开发者_如何学Goanks for any suggestions. Frank Ogiamien


Don't call it zip! You're masking the builtin.

Also, is zip.close() really inside your for loop? It shouldn't be.

You should use the extract method of the ZipFile object, so you don't need to read the whole file into memory.

Instead of

file(join("C:\\Incoming\\", name), 'wb').write(zip.read(each))

do

zip.extract(each, "C:\\Incoming\\")

Edit: This was added in 2.6, as was extractall if 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.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜