Zip Two Files into a Single Zip File

  • Share this:

Code introduction


This function zips two specified files into a single zip file.


Technology Stack : os, zipfile

Code Type : File compression

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2, output):
    import os
    import zipfile
    with zipfile.ZipFile(output, 'w') as z:
        z.write(file1, os.path.basename(file1))
        z.write(file2, os.path.basename(file2))
    return f"Files {file1} and {file2} have been zipped into {output}"                
              
Tags: