You can download this code by clicking the button below.
This code is now available for download.
This function zips two directories into two zip files and returns the names of the zipped files.
Technology Stack : os, zipfile
Code Type : File processing
Code Difficulty : Intermediate
def zipfiles(file1, file2):
import os
import zipfile
def zip_dir(dir_to_zip, zip_filename):
with zipfile.ZipFile(zip_filename, 'w') as zipf:
for root, dirs, files in os.walk(dir_to_zip):
for file in files:
zipf.write(os.path.join(root, file), os.path.relpath(os.path.join(root, file), dir_to_zip))
zip_dir(os.path.dirname(file1), file1)
zip_dir(os.path.dirname(file2), file2)
return f"{file1}.zip" if file1.endswith('.zip') else file1 + '.zip', f"{file2}.zip" if file2.endswith('.zip') else file2 + '.zip'