Empty directories with zipfile

Fuzzyman michael at foord.net
Fri Jun 11 16:53:49 CEST 2004

Peter Hansen <peter at engcorp.com> wrote in message news:<35udnWPiL4B6DVTdRVn-ug at powergate.ca>...
> Fuzzyman wrote:
> > Peter Hansen <peter at engcorp.com> wrote in message news:<RY6dncvHheqqvlrdRVn-sw at powergate.ca>...
> >>http://groups.google.com/groups?q=comp.lang.python+zipfile+empty+directory
> > 
> > Ha... thanks....
> > Worked a treat.
> For the record, which solution did you use?  I see at least
> two alternatives in the second and third hits to the above
> search (attribute 16, attribute 48, and maybe another option
> involving another argument... didn't read it all in detail).
> -Peter

The simplest one ! Which works fine.....

from zipfile import ZipFile, ZipInfo
zip = ZipFile(zipfilename, 'w')
zi = ZipInfo(emptydirname+'/')     

Create a ZipInfo instance where the filename ends in '/' and use the
writstr method.

Only other pitfall - You have to be careful when extracting - test if
names in the zip.namelist() end with '/' and create the directory
rather than trying to write the file......




More information about the Python-list mailing list