You can download this code by clicking the button below.
This code is now available for download.
This function compresses all files in a specified folder into a single zip file.
Technology Stack : os, zipfile
Code Type : Function
Code Difficulty : Intermediate
def zip_file(folder_path, zip_filename):
import os
import zipfile
# Create a zipfile object
zipf = zipfile.ZipFile(zip_filename, 'w')
# Loop through all files and add them to the zipfile
for foldername, subfolders, filenames in os.walk(folder_path):
for filename in filenames:
file_path = os.path.join(foldername, filename)
zipf.write(file_path, os.path.relpath(file_path, folder_path))
# Close the zipfile
zipf.close()