You can download this code by clicking the button below.
This code is now available for download.
This function combines two files into a single zip file and removes the temporary directory after completion.
Technology Stack : os, shutil, zipfile
Code Type : File processing
Code Difficulty : Intermediate
def zip_file(file1, file2):
import os
import shutil
import zipfile
def create_zip(zip_name, *files):
with zipfile.ZipFile(zip_name, 'w') as zipf:
for file in files:
zipf.write(file, os.path.basename(file))
def remove_directory(directory):
shutil.rmtree(directory)
zip_name = "combined.zip"
directory = "temp_dir"
os.makedirs(directory, exist_ok=True)
shutil.copy(file1, directory)
shutil.copy(file2, directory)
create_zip(zip_name, directory)
remove_directory(directory)
return zip_name