Combining Files into a Zip Archive

  • Share this:

Code introduction


Compress two files into a single zip file.


Technology Stack : os, zipfile

Code Type : File processing

Code Difficulty : Intermediate


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

    def create_zip_file(zip_path, files):
        with zipfile.ZipFile(zip_path, 'w') as zipf:
            for file in files:
                zipf.write(file, os.path.basename(file))

    zip_path = 'combined.zip'
    create_zip_file(zip_path, [file1, file2])
    return zip_path                
              
Tags: