Created
November 29, 2016 11:49
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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