Merging Two Files into a Single File

  • Share this:

Code introduction


This function merges the contents of two files into a new file.


Technology Stack : File operation, binary read and write

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2, output):
    import shutil
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2, open(output, 'wb') as output_file:
        shutil.copyfileobj(f1, output_file)
        shutil.copyfileobj(f2, output_file)