Compress and Rename Two Files into a ZIP

  • Share this:

Code introduction


Compresses two files into a ZIP file and then renames the ZIP file.


Technology Stack : os, zipfile, shutil

Code Type : File processing

Code Difficulty : Intermediate


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

    with zipfile.ZipFile('output.zip', 'w') as zipf:
        zipf.write(file1, os.path.basename(file1))
        zipf.write(file2, os.path.basename(file2))
    shutil.move('output.zip', 'zipped_output.zip')                
              
Tags: