You can download this code by clicking the button below.
This code is now available for download.
This function zips two files into one and renames the original file to a zip file, deleting the other file.
Technology Stack : os, zipfile, shutil
Code Type : Function
Code Difficulty : Intermediate
def zip_file(file1, file2):
import os
import zipfile
import shutil
def get_zip_filename(filename):
return filename.split('.')[0] + '.zip'
zip_filename = get_zip_filename(file1)
with zipfile.ZipFile(zip_filename, 'w') as zipf:
zipf.write(file1, arcname=os.path.basename(file1))
zipf.write(file2, arcname=os.path.basename(file2))
shutil.move(zip_filename, file1)
os.remove(file2)
return zip_filename