You can download this code by clicking the button below.
This code is now available for download.
This function takes a list of files and an output directory, and compresses all files in the list into a zip format, saving it in the output directory.
Technology Stack : os, zipfile, shutil
Code Type : File compression
Code Difficulty : Intermediate
def zipfiles(file_list, output_dir):
import os
import zipfile
import shutil
# Ensure output directory exists
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# Create a zip file and add files to it
zip_filename = os.path.join(output_dir, 'archive.zip')
with zipfile.ZipFile(zip_filename, 'w') as zipf:
for file in file_list:
zipf.write(file, os.path.basename(file))
return zip_filename