You can download this code by clicking the button below.
This code is now available for download.
This function takes a file path and an output path, compresses the input file into a zip format, and saves it to the specified output path.
Technology Stack : os, zipfile
Code Type : File processing
Code Difficulty : Intermediate
def zip_file(file_path, output_path):
import os
import zipfile
# Check if the file exists
if not os.path.isfile(file_path):
raise FileNotFoundError("The file does not exist.")
# Create a zipfile object
with zipfile.ZipFile(output_path, 'w') as zipf:
# Add the file to the zipfile
zipf.write(file_path, os.path.basename(file_path))
return output_path