Compress and Move Files into ZIP Format

  • Share this:

Code introduction


This function accepts two filenames as arguments, compresses them into a ZIP file, and moves the final ZIP file to the current working directory.


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', os.path.join(os.getcwd(), 'compressed_files.zip'))                
              
Tags: