Zipping and Moving Two Files into an Output Directory

  • Share this:

Code introduction


This function zips two files into a single zip file and moves the resulting file to the specified output directory.


Technology Stack : os, zipfile, shutil

Code Type : Function

Code Difficulty : Intermediate


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

    # 创建一个zip文件
    with zipfile.ZipFile(output, 'w') as z:
        # 添加第一个文件
        z.write(file1, os.path.basename(file1))
        # 添加第二个文件
        z.write(file2, os.path.basename(file2))

    # 使用shutil来移动zip文件,确保它在输出目录中
    shutil.move(output, output)                
              
Tags: