Combining File Contents in Binary Mode

  • Share this:

Code introduction


Merge the contents of two files into a new file, using binary read and write modes.


Technology Stack : File read and write

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zip_file(file1, file2, output):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        with open(output, 'wb') as f_out:
            f_out.write(f1.read())
            f_out.write(f2.read())
        return f_out                
              
Tags: