zip function for python

Luca luca.colombi.it at gmail.com
Thu Jan 17 07:32:32 EST 2008


Hi, Ive written this easy and fast function cause there it was
impossible for me to find one that zip a directory without external
libraries:

import zipfile
import sys
import os

def zipdir(zipPath,directory="./"):
 """Store the cdontent of directory to a zipPath file, if directory is
not given stores the content of the current dir
( luca.colombi.it at gmail.com )"""
 directory=os.path.realpath(directory)
 zipObject = zipfile.ZipFile(zipPath, 'w')
 for root, dirs, files in os.walk(directory):
  for file in files:
   arcfile=root[len(os.path.commonprefix((directory, root)))
+1:]+"/"+file #retrieves the inner relative file path
   filepath=os.path.join(root,file)
   zipObject.write(filepath,arcfile)
 zipObject.close()
 return zipObject #for optional further elaborations



More information about the Python-list mailing list