Create ZIP File from Two Files

  • Share this:

Code introduction


This function merges two files into a single ZIP file.


Technology Stack : zipfile, os

Code Type : Function

Code Difficulty : Intermediate


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

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

    return "combined.zip"                
              
Tags: