Question: In Python: Create a program called Dif. This program should prompt the user for the names of two text files and compare the contents of
In Python:
Create a program called Dif. This program should prompt the user for the names of two text files and compare the contents of the two files to see if they are the same. If they are, the program should simply output Yes to the console. If they are not, the script should output No, followed by the first lines of each file that differ from each other. The input loop should read and compare lines from each file. The loop should break as soon as a pair of different lines is found.
**Files were indluded with the assignment for testing but you can just create 3 simple .txt files in your src file in the python module (Text1.txt and Text2.txt are the same while Text3.txt is a little different)
**Below is the code I have so far. I am having trouble geting the program to run past the section to answer "yes" if they are the same. I am unable to get it to complete the code (answering "NO', iterating, and comparing) when I test it with the .txt files that are different from one another. have tried tweeking it to get the syntax right but get other errors such as "I" is not defined.
PYTHON CODE:
import atexit
#Read names of both files print("Enter two files to be compared below") userName1 = input("Enter the name of the first file: ") userName2 = input("Enter the name of the second file: ")
#Open each file f1 = open(userName1, 'r') f2 = open(userName2, 'r')
#read all the lines into a list d1 = f1.readlines() d2 = f2.readlines()
#If they are equivalent, print "Yes" else print "No",iterate, and compare. if d1 == d2: print("Yes") atexit elif for i in range(0,min(len(d1),len(d2))): if d1[i]!=d2[i]: print("No") print(d1[i])
print(d2
[i]
Console 3 kterminated> Dif.py [G schooNcpt 135 programming in python Python python.exe] File "D thon workspace\Chapter12Assignment\src\Dif.", line 27 elif for i in range (e,min(len (d1),len (d2))) SyntaxError: invalid syntax 1e''" 2 Created on Noy 3, 2018 4 author: ABLand 6 7 import atexit 8 9 10 #Read names of both files 011 print"Enter two files to be compared below" 012 userName! input("Enter the name of the first file: ") 13 userName2 input("Enter the name of the second file: ") 14 15 #Open each file 16 f1-open (userNamel, r') 017 f2- open (userName2, 'r') 18 19 20 d1- f1.readlines () 21 d2- f2.readlines() #read all the lines into a list 23 #If they are equivalent, print "Yes" else print "No" , iterate, and compare 25 26 print("Yes") atexit 027 elif for i in cange (e,minlen(d1),len(d2))): 329 331 if dI[i]!-d2[i]: Rcint("No") 28 pcint(d2[i]) 32
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
