Combining Files into a Zip File in Python

  • Share this:

Code introduction


This code defines a function named zipfiles that accepts two filenames as arguments and combines these files into a new zip file.


Technology Stack : The packages and technologies used in the code[English]

Code Type : Function

Code Difficulty : Advanced


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

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

    print("The files have been combined into a single zip file.")