How can I create a zip archive of a directory structure in Python?
For a concise way to retain the folder hierarchy under the parent directory to be archived:
import glob
import zipfile
with zipfile.ZipFile(fp_zip, "w", zipfile.ZIP_DEFLATED) as zipf:
for fp in glob(os.path.join(parent, "**/*")):
base = os.path.commonpath([parent, fp])
zipf.write(fp, arcname=fp.replace(base, ""))
If you want, you could change this to use pathlib
for file globbing.