Created
September 8, 2021 09:34
-
-
Save vikramsoni2/a36bc0d0f95ec451326da78f91b4b7e5 to your computer and use it in GitHub Desktop.
zip entire directory in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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