Comparing File Lines with Python

  • Share this:

Code introduction


This function prints the corresponding lines from two files, typically used to compare the contents of two files.


Technology Stack : File reading and writing, iterator, zip function

Code Type : File operation

Code Difficulty : Intermediate


                
                    
def zip_file_files(file1, file2):
    with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
        for line1, line2 in zip(f1, f2):
            print(line1.decode(), line2.decode())