Skip to content

Instantly share code, notes, and snippets.

@swayson
Created November 29, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save swayson/74dcfca592e080f615edcfd574eaff7a to your computer and use it in GitHub Desktop.
Save swayson/74dcfca592e080f615edcfd574eaff7a to your computer and use it in GitHub Desktop.
Simple example on how to use Python Zipfile to make a compressed archive. Here additional care is taken to provide an arcname, such that the root is avoided in the archive.
files = [path/to/file, path/to/file2, path/to/file3]
with zipfile.ZipFile('/tmp/test.zip', 'w', zipfile.ZIP_DEFLATED) as out_file:
for rel_filename in files:
absname = os.path.abspath(rel_filename)
root = os.path.dirname(absname)
filename = os.path.relpath(rel_filename, root)
out_file.write(rel_filename, filename)
print(absname, root, filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment