Empty directories with zipfile

F

Fuzzyman

I have created a set of classes that will profile a file structure and
record all changes as a simple markup and single zipfile of all
new/modified files. I can't get the zipfile module to archive an empty
directory - you need to give it a filename. I know the zip format will
support it as winzip will allow it... I looked at the zipfile module
source - and I'm sure it's not impenetrable but I don't wish to learn
the intricasies of the zip format just yet :)

Anyone got any ideas - beyond extending the markup to record *all*
empty directories created as well...... ??

Regards,

Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 
P

Peter Hansen

Fuzzyman said:
I have created a set of classes that will profile a file structure and
record all changes as a simple markup and single zipfile of all
new/modified files. I can't get the zipfile module to archive an empty
directory - you need to give it a filename. I know the zip format will
support it as winzip will allow it...

Past discussions available from your friend Google:

http://groups.google.com/groups?q=comp.lang.python+zipfile+empty+directory
 
P

Peter Hansen

Fuzzyman said:
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
 
F

Fuzzyman

Peter Hansen said:
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+'/')
zip.writestr(zi,'')
zip.close()

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......

Regards,


Fuzzy

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top