Compress Two Files into a Zip File

  • Share this:

Code introduction


This function compresses two files into a new zip file.


Technology Stack : os, zipfile

Code Type : File processing

Code Difficulty : Intermediate


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

    if not os.path.exists(file1) or not os.path.exists(file2):
        raise ValueError("One or both of the input files do not exist.")

    with zipfile.ZipFile(output, 'w') as zipf:
        zipf.write(file1)
        zipf.write(file2)

    return output                
              
Tags: