Merge and Compress Two Text Files into a Zip File

  • Share this:

Code introduction


This function reads the contents of two text files and merges them into a single zip file.


Technology Stack : zipfile

Code Type : File manipulation and compression

Code Difficulty : Intermediate


                
                    
def zip_file_files(file1, file2, output):
    """
    将两个文件的内容合并并压缩为zip文件。
    """
    with open(file1, 'r') as f1, open(file2, 'r') as f2, zipfile.ZipFile(output, 'w') as z:
        z.writestr('file1.txt', f1.read())
        z.writestr('file2.txt', f2.read())                
              
Tags: