Compress and Relocate Files into a Zip Archive

  • Share this:

Code introduction


Compress two files into a zip file and move the output file to the parent directory of the current directory.


Technology Stack : os, zipfile, shutil

Code Type : File processing

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2, output):
    import os
    import zipfile
    import shutil
    
    with zipfile.ZipFile(output, 'w') as zipf:
        zipf.write(file1, os.path.basename(file1))
        zipf.write(file2, os.path.basename(file2))
    shutil.move(output, os.path.dirname(output) + os.sep + output)                
              
Tags: