[Tutor] memory error

Oxymoron moron.oxy at gmail.com
Sat Mar 7 02:16:00 CET 2009


Hello,

On Sat, Mar 7, 2009 at 11:03 AM, Harris, Sarah L
<sarah.l.harris at jpl.nasa.gov> wrote:
> import zipfile, glob, os
> os.chdir('E:\\test1')
> from os.path import isfile
> fname=filter(isfile, glob.glob('*.zip'))
> for fname in fname:
>     zipnames=filter(isfile, glob.glob('*.zip'))
>     for zipname in zipnames:
>         zf=zipfile.ZipFile(zipname, 'r')
>         for zfilename in zf.namelist():
>             newFile=open(zfilename, 'wb')
>             newFile.write(zf.read(zfilename))
>             newFile.close()
>     zf.close()

I assume this zf,close() call is indented one more level, so it's
inline with the zf= zipfile.ZipFile(...)

As for the memory error, zf.read(zfilename) returns the entire set of
bytes based on the documentation of the archive member. There's this
call:

http://docs.python.org/library/zipfile.html#zipfile.ZipFile.open

which returns a file-like object
(http://docs.python.org/library/stdtypes.html#file-objects), in
particular you can use read(num_bytes) on the returned file like
object till you encounter EOF so you can do step-by-step reads (and
writes) instead of reading the entire file into memory.

-- Kamal

-- 
There is more to life than increasing its speed.
 -- Mahatma Gandhi


More information about the Tutor mailing list