Skip to content

Instantly share code, notes, and snippets.

@stucka
Created April 17, 2020 19:47
Show Gist options
  • Save stucka/5cda059627d3ed5fa950420922719e06 to your computer and use it in GitHub Desktop.
Save stucka/5cda059627d3ed5fa950420922719e06 to your computer and use it in GitHub Desktop.
Save stuff to ZIPs
import zipfile
import os
import glob
zipfilename = "/Users/mstucka/Dropbox/somedata.zip"
sourcedir = "somedata/"
filesalreadygot = []
if os.path.exists(zipfilename):
with zipfile.ZipFile(zipfilename, 'r') as myzip:
filesalreadygot = myzip.namelist()
filesavailable = list(glob.glob(sourcedir + "*"))
with zipfile.ZipFile(zipfilename, "a") as myzip: # , compression="ZIP_DEFLATED", compresslevel=9
for fileavailable in filesavailable:
basefilename = fileavailable.replace("\\", "/").replace(sourcedir, "")
if basefilename not in filesalreadygot:
print(f"Adding {basefilename}")
myzip.write(fileavailable, arcname=basefilename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment