Compress and Rename Two Files into #compressed_files.zip#

  • Share this:

Code introduction


This function compresses two files into a single zip file and renames the zip file to 'compressed_files.zip'.


Technology Stack : os, zipfile, shutil

Code Type : File processing

Code Difficulty : Intermediate


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

    zip_path = 'output.zip'
    with zipfile.ZipFile(zip_path, 'w') as zipf:
        zipf.write(file1, os.path.basename(file1))
        zipf.write(file2, os.path.basename(file2))

    shutil.move(zip_path, 'compressed_files.zip')
    return 'compressed_files.zip'                
              
Tags: