Combining File Contents into a Single File

  • Share this:

Code introduction


Combine the contents of two files into a new file.


Technology Stack : File operation

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2, output):
    """
    将两个文件的内容压缩并保存到一个新的文件中。

    :param file1: 第一个文件的路径
    :param file2: 第二个文件的路径
    :param output: 输出文件的路径
    """
    with open(file1, 'r') as f1, open(file2, 'r') as f2:
        content = f1.read() + f2.read()
    with open(output, 'w') as f_out:
        f_out.write(content)

                 
              
Tags: