Python script to compare two text files

This is a simple python script to compare two text files line by line and output only the lines that are different.

Program Analysis

The program asks the user to input the names of the two files to compare. It will then open the files in read only mode and reads one line at a time from each file and compares them after stripping off any trailing whitespaces, which means we are ignoring new-line and spaces at the end of the line.

If the lines are different then it is printed out along with the corresponding line number and the prefix '>' or '<' to indicate the file.

An additional check is also carried out to determine if that specific line exist only on one file in which case it is indicated with a + sign in the output.

Program Source

# Ask the user to enter the names of files to compare
fname1 = input("Enter the first filename: ")
fname2 = input("Enter the second filename: ")

# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)

# Print confirmation
print("-----------------------------------")
print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
print("-----------------------------------")

# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()

# Initialize counter for line number
line_no = 1

# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':

    # Strip the leading whitespaces
    f1_line = f1_line.rstrip()
    f2_line = f2_line.rstrip()
    
    # Compare the lines from both file
    if f1_line != f2_line:
        
        # If a line does not exist on file2 then mark the output with + sign
        if f2_line == '' and f1_line != '':
            print(">+", "Line-%d" % line_no, f1_line)
        # otherwise output the line on file1 and mark it with > sign
        elif f1_line != '':
            print(">", "Line-%d" % line_no, f1_line)
            
        # If a line does not exist on file1 then mark the output with + sign
        if f1_line == '' and f2_line != '':
            print("<+", "Line-%d" % line_no, f2_line)
        # otherwise output the line on file2 and mark it with < sign
        elif f2_line != '':
            print("<", "Line-%d" %  line_no, f2_line)

        # Print a blank line
        print()

    #Read the next line from the file
    f1_line = f1.readline()
    f2_line = f2.readline()


    #Increment line counter
    line_no += 1

# Close the files
f1.close()
f2.close()

Program Output

Consider two text files file1.txt and file2.txt with the following contents

file1.txt

opentechguides website contains
tutorials and howto articles
on topics such as Linux
Windows, databases etc.,

file2.txt

opentechguides website contains
tutorials and howto articles
on topics such as Linux
Windows, databases , networking
programming and web development.

If you compare these two files using our python script, the output will be as shown below:


 Enter the first filename: file1.txt
 Enter the second filename: file2.txt
 -----------------------------------
 Comparing files 
  > file1.txt
  < file2.txt
 -----------------------------------
 > Line-4 Windows, databases etc.,
 < Line-4 Windows, databases , networking

 <+ Line-5 programming and web development.


Post a comment

Name

Your Comment

Email (We dont publish it)

Comments

gnanasekar | April 29, 2016 9:52 AM |

When I run the above script in the name of sekar.py, it was asking file name,when I entered the file name as file1.txt I could see that error displays as below Enter the first filename: file1.txt Traceback (most recent call last): File "C:\Python27\Scripts\PY\sekar.py", line 2, in fname1 = input("Enter the first filename: ") File "", line 1, in NameError: name 'f