Compress and Zip Two Files into a Single Archive

  • Share this:

Code introduction


This function compresses two files into a zip format and outputs them to the specified file path.


Technology Stack : os, zipfile

Code Type : File operation

Code Difficulty : Intermediate


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

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

    return output                
              
Tags: