Skip to content

Instantly share code, notes, and snippets.

@vikramsoni2
Created September 8, 2021 09:34
Show Gist options
  • Save vikramsoni2/a36bc0d0f95ec451326da78f91b4b7e5 to your computer and use it in GitHub Desktop.
Save vikramsoni2/a36bc0d0f95ec451326da78f91b4b7e5 to your computer and use it in GitHub Desktop.
zip entire directory in python
from zipfile import ZipFile
import os
from os.path import basename
def zip_dir(dirname, zipfilename):
with ZipFile(zipfilename, 'w') as zipObj:
# Iterate over all the files in directory
for folderName, subfolders, filenames in os.walk(dirname):
for filename in filenames:
#create complete filepath of file in directory
filePath = os.path.join(folderName, filename)
# Add file to zip
zipObj.write(filePath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment