Compress Two Files into a Zip File

  • Share this:

Code introduction


This function compresses two files into one zip file. The first and second arguments are the paths of the files to be compressed.


Technology Stack : os, zipfile

Code Type : File processing

Code Difficulty : Intermediate


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

    base_name = os.path.splitext(os.path.basename(file1))[0]
    zip_filename = f"{base_name}.zip"
    with zipfile.ZipFile(zip_filename, 'w') as zipf:
        zipf.write(file1)
        zipf.write(file2)
    return zip_filename                
              
Tags: