Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Recursive zip python
# Original:
# 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)
_, name = os.path.split(path)
archive.write(path, name)
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)
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