Compress Two Files into a Zip Archive

  • Share this:

Code introduction


This function takes two filenames and an output filename, and compresses these two files into a zip file.


Technology Stack : zipfile, shutil

Code Type : File compression

Code Difficulty : Intermediate


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

    with zipfile.ZipFile(output, 'w') as z:
        z.write(file1, arcname=file1)
        z.write(file2, arcname=file2)

    print(f"Files {file1} and {file2} have been zipped into {output}")                
              
Tags: