0

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

2
  • what is the size of the zip objects you're trying to unzip ? Commented Jul 27, 2011 at 18:41
  • The .zip files I'm hoping to extract from range in size up to 500meg. My test example is 73meg. Commented Jul 27, 2011 at 21:52

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, I changed my "zip" to "z" and tried your suggestion of using z.extract(each, "C:\\Incoming\\") however I get a new error: AttributeError: ZipFile instance has no attribute 'extract'" Is this because my version 2.5 of py is too old? Oh, my zip.close() wasn't really inside the for loop. typo.
agf, thanks very much for your time. Your comments were very helpful. It looks like I'm better off upgrading to 2.6 if I want to keep the code short and simple. Frank

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.