Compress Two Files into a Single Zip File

  • Share this:

Code introduction


This function compresses two files into a single zip file.


Technology Stack : os, zipfile

Code Type : File operation

Code Difficulty : Intermediate


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

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

    return "combined.zip"                
              
Tags: