Merging Two Folders into a Single ZIP File

  • Share this:

Code introduction


Combine the contents of two folders into one ZIP file.


Technology Stack : os, zipfile

Code Type : File compression

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    import os
    import zipfile

    def zipdir(path, ziph):
        for root, dirs, files in os.walk(path):
            for file in files:
                ziph.write(os.path.join(root, file), 
                           os.path.relpath(os.path.join(root, file), 
                                           path))

    with zipfile.ZipFile('combined.zip', 'w') as z:
        zipdir(file1, z)
        zipdir(file2, z)

                 
              
Tags: