Create Zip File from Directory

  • Share this:

Code introduction


This function creates a zip file by compressing all files in the specified directory into the zip file.


Technology Stack : zipfile, os

Code Type : Function

Code Difficulty : Intermediate


                
                    
def zipfile_create(directory, filename):
    import zipfile
    import os

    with zipfile.ZipFile(filename, 'w') as zipf:
        for root, dirs, files in os.walk(directory):
            for file in files:
                zipf.write(os.path.join(root, file), arcname=file)

    return filename                
              
Tags: