Create Zip File from Folder

  • Share this:

Code introduction


This function creates a zip file, packaging all files in the specified folder into the zip file.


Technology Stack : zipfile, os, json

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zipfile_create(input_folder, output_file):
    import zipfile
    import os
    import json

    with zipfile.ZipFile(output_file, 'w') as zipf:
        for foldername, subfolders, filenames in os.walk(input_folder):
            for filename in filenames:
                file_path = os.path.join(foldername, filename)
                zipf.write(file_path, os.path.relpath(file_path, input_folder))

    return output_file                
              
Tags: