Created
July 17, 2023 12:56
-
-
Save nielsvaes/1d0f72250d4350ea79dfeeb7a17e685e to your computer and use it in GitHub Desktop.
Zip a folder to a file
This file contains hidden or 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
| import os | |
| import zipfile | |
| def make_zipfile(output_filename, source_dir): | |
| relroot = os.path.abspath(os.path.join(source_dir, os.pardir)) | |
| with zipfile.ZipFile(output_filename, "w", zipfile.ZIP_DEFLATED) as zip: | |
| for root, dirs, files in os.walk(source_dir): | |
| zip.write(root, os.path.relpath(root, relroot)) | |
| for file in files: | |
| filename = os.path.join(root, file) | |
| if os.path.isfile(filename): | |
| arcname = os.path.join(os.path.relpath(root, relroot), file) | |
| zip.write(filename, arcname) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment