Combining Files into a Zip Archive

  • Share this:

Code introduction


Compress two files into one zip file.


Technology Stack : os, zipfile

Code Type : File operation

Code Difficulty : Intermediate


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

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

    return "combined.zip"                
              
Tags: