139k views
3 votes
Design the program that reads two files and compares their contents. The program calls a method that reads the file one line at a time. If the lines are the same the program skips the line. If the lines are different then the program prints the line number followed by the different lines from each file.

User Shivanka
by
5.1k points

1 Answer

5 votes

Answer:

file1 = []

file2 = []

fileone = "lyric1.txt"

filetwo = "lyric2.txt"

def linereader( myfile, mylist):

for line in open(myfile, "r") as file:

line_read = readlines( line)

mylist.append( line_read)

file.close()

linereader( fileone, file1)

linereader( filetwo, file2)

# Assuming both files have same number of lines.

for index, item in enumerate(zip(file1, file2)):

if item[0] != item[1]:

print( index + 1, item)

Step-by-step explanation:

The python code compares two files and returns the number and the lines from both files that do not match. The source code opens the files lyric1.txt and lyric2.txt reading each lines to different lists which are iterated to compare its items returning the number of the line and the unequal lines.

User MYE
by
5.4k points