How to compress a folder and all of its sub directories and files into a zip file?

Peter Szinek peter at rt.sk
Tue Jun 28 13:29:23 EDT 2005


Hi,

What about this:

import os,zipfile
from os.path import join


zip = zipfile.ZipFile("myzipfile.zip", 'w')
for root, dirs, files in os.walk('.'):
     for fileName in files:
         zip.write(join(root,fileName))
zip.close()

Maybe it zips also the myzipfile.zip ;-)
Probably this is not needed, so an additional test (something like 
fileName != 'myfile.zip' would be needed.

HTH,
Peter

could ildg wrote:
> I want to compress a folder and all of its sub files including empty folders
> into a zip file. what's the pythonic way to do this?
> 
> Thanks in advance.




More information about the Python-list mailing list