Skip to content

Instantly share code, notes, and snippets.

@nielsvaes
Created July 17, 2023 12:56
Show Gist options
  • Select an option

  • Save nielsvaes/1d0f72250d4350ea79dfeeb7a17e685e to your computer and use it in GitHub Desktop.

Select an option

Save nielsvaes/1d0f72250d4350ea79dfeeb7a17e685e to your computer and use it in GitHub Desktop.
Zip a folder to a file
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