Zipping Two Files into a Single Zip and Renaming Output

  • Share this:

Code introduction


This function zips two files into a single zip file and then renames the output file.


Technology Stack : os, zipfile, shutil

Code Type : File operation

Code Difficulty : Intermediate


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

    with zipfile.ZipFile(output, 'w') as z:
        z.write(file1, os.path.basename(file1))
        z.write(file2, os.path.basename(file2))

    shutil.move(output, output.replace('.zip', '_zipped.zip'))                
              
Tags: