zipfile: grabbing whole directories

Daniel Nogradi nogradi at gmail.com
Tue May 1 07:20:52 EDT 2007


> The built in zipfile.write doesn't seem to like  taking a directory instead
> of  a filename.
>
> for example:
> for each in listofdir:
>                 archive.write(each)
>
> blows up when one of the items listed in listofdir is a subdirectory.
>
>   File "/usr/local/lib/python2.4/zipfile.py", line 405, in write
>     fp = open(filename, "rb")
>
> is there a mode or a '-r' style param I am missing?

I guess what you really want is recursively archive every file in a
directory and its subdirectories. If that is the case you can use
os.walk to iterate over all such files so you can archive all of them
while the directory structure will be preserved.

HTH,
Daniel



More information about the Python-list mailing list