Combining and Compressing File Contents

  • Share this:

Code introduction


This function combines the contents of two text files and writes the combined content to a new compressed file in byte format.


Technology Stack : Built-in libraries (open, encode, with)

Code Type : Compressed file

Code Difficulty : Intermediate


                
                    
def zipfiles(file1, file2):
    """
    将两个文件的内容压缩并保存到新文件中。
    """
    with open(file1, 'r') as f1, open(file2, 'r') as f2:
        content1, content2 = f1.read(), f2.read()
        combined_content = content1 + content2

    with open('combined.zip', 'wb') as f:
        f.write(combined_content.encode())