Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Recursive zip python
# Original:
# http://stackoverflow.com/a/6078528
#
# Call `zipit` with the path to either a directory or a file.
# All paths packed into the zip are relative to the directory
# or the directory of the file.
def zipit(path, archname):
archive = zipfile.ZipFile(archname, "w", zipfile.ZIP_DEFLATED)
if os.path.isdir(path):
_zippy(path, path, archive)
else:
_, name = os.path.split(path)
archive.write(path, name)
archive.close()
def _zippy(base_path, path, archive):
paths = os.listdir(path)
for p in paths:
p = os.path.join(path, p)
if os.path.isdir(p):
_zippy(base_path, p, archive)
else:
archive.write(p, os.path.relpath(p, base_path))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment