Skip to content

Instantly share code, notes, and snippets.

@felixSchl
Last active October 28, 2020 09:10
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save felixSchl/d38b455df8bf83a78d3d to your computer and use it in GitHub Desktop.
Save felixSchl/d38b455df8bf83a78d3d to your computer and use it in GitHub Desktop.
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