zip function for python

L

Luca

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
( (e-mail address removed) )"""
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
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top