October 19, 2024

import os
import zipfile

def zip_directory(
folder_path: str,
zip_path: str
):
with zipfile.ZipFile(zip_path, ‘w’, zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(folder_path):
for file in files:
full_path = os.path.join(root, file)
relative_path = os.path.relpath(
full_path,
os.path.commonpath([folder_path])
)
# add file to zip
zipf.write(
full_path,
arcname=relative_path
)

zip_directory(‘./images’, ‘output.zip’)