You can download this code by clicking the button below.
This code is now available for download.
This function compresses a list of files into a zip file and saves it in the specified folder. The file name includes a timestamp for distinction.
Technology Stack : os, zipfile, datetime
Code Type : File compression
Code Difficulty : Intermediate
def zipfiles(file_list, output_folder):
"""
Compress multiple files into a zip file in a given folder.
"""
import os
import zipfile
from datetime import datetime
# Ensure output folder exists
if not os.path.exists(output_folder):
os.makedirs(output_folder)
# Create a timestamp for the zip file name
timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
zip_filename = f"{output_folder}/files_{timestamp}.zip"
# Create a zip file and add files to it
with zipfile.ZipFile(zip_filename, 'w') as zipf:
for file in file_list:
zipf.write(file, arcname=os.path.basename(file))
return zip_filename