This is for my Programming and Python class, and I didn\'t understand what my te
ID: 3705474 • Letter: T
Question
This is for my Programming and Python class, and I didn't understand what my teacher wanted me to do!! I need help reall bad otherwise I willl fail my class.
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.
The links below contain text for testing. Text1 and Text2 are the same file, Text3 is slightly different.
Here is Text 1
Fans, for the past two weeks you have been reading about a bad break I
got. Yet today I consider myself the luckiest man on the face of the
earth. I have been in ballparks for seventeen years and have never
received anything but kindness and encouragement from you fans.
Look at these grand men. Which of you wouldn’t consider it the highlight
of his career to associate with them for even one day?
Sure, I’m lucky. Who wouldn’t consider it an honor to have known Jacob
Ruppert – also the builder of baseball’s greatest empire, Ed Barrow – to
have spent the next nine years with that wonderful little fellow Miller
Huggins – then to have spent the next nine years with that outstanding
leader, that smart student of psychology – the best manager in baseball
today, Joe McCarthy!
Sure, I’m lucky. When the New York Giants, a team you would give your
right arm to beat, and vice versa, sends you a gift, that’s something!
When everybody down to the groundskeepers and those boys in white coats
remember you with trophies, that’s something.
When you have a wonderful mother-in-law who takes sides with you in
squabbles against her own daughter, that’s something. When you have a
father and mother who work all their lives so that you can have an
education and build your body, it’s a blessing! When you have a wife who
has been a tower of strength and shown more courage than you dreamed
existed, that’s the finest I know.
So I close in saying that I might have had a tough break – but I have an
awful lot to live for!
Lou Gehrig, 1939
Text 2
Fans, for the past two weeks you have been reading about a bad break I
got. Yet today I consider myself the luckiest man on the face of the
earth. I have been in ballparks for seventeen years and have never
received anything but kindness and encouragement from you fans.
Look at these grand men. Which of you wouldn’t consider it the highlight
of his career to associate with them for even one day?
Sure, I’m lucky. Who wouldn’t consider it an honor to have known Jacob
Ruppert – also the builder of baseball’s greatest empire, Ed Barrow – to
have spent the next nine years with that wonderful little fellow Miller
Huggins – then to have spent the next nine years with that outstanding
leader, that smart student of psychology – the best manager in baseball
today, Joe McCarthy!
Sure, I’m lucky. When the New York Giants, a team you would give your
right arm to beat, and vice versa, sends you a gift, that’s something!
When everybody down to the groundskeepers and those boys in white coats
remember you with trophies, that’s something.
When you have a wonderful mother-in-law who takes sides with you in
squabbles against her own daughter, that’s something. When you have a
father and mother who work all their lives so that you can have an
education and build your body, it’s a blessing! When you have a wife who
has been a tower of strength and shown more courage than you dreamed
existed, that’s the finest I know.
So I close in saying that I might have had a tough break – but I have an
awful lot to live for!
Lou Gehrig, 1939
Text 3
Fans, for the past two weeks you have been reading about a bad break I
got. Yet today I consider myself the luckiest man on the face of the
earth. I have been in ballparks for seventeen years and have never
received anything but kindness and encouragement from you fans.
Look at these grand men. Which of you wouldn’t consider it the highlight
of his career to associate with them for even one day?
Sure, I’m lucky. Who wouldn’t consider it an honor to have known Jacob
Ruppert – also the builder of baseball’s greatest empire, Ed Barrow – to
have spent the next nine years with that wonderful little fellow Miller
Huggins – then to have spent the next nine years with that outstanding
leader, that smart student of psychology – the best manager in baseball
today, Joe McCarthy!
Sure, I’m lucky. When the New York Giants, a team you would give your
right arm to beat, and vice versa, sends you a gift, that’s something!
When everybody down to the groundskeepers and those boys in white coats
remember you with trophies, that’s something.
When you have a wonderful mother-in-law who takes sides with you in
squabbles against her own daughter, that’s something. When you have a
father and mother who work all their lives so that you can have an
education and build your body, it’s a blessing! When you have a wife who
has been a tower of strength and shown more courage than you dreamed
existed, that’s the finest I know.
So I close in saying that I might have had a tough break – but I have an
awful lot to live for!
Thanks for your help!!
Explanation / Answer
# 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,"r")
f2 = open(fname2,"r")
# Print confirmation
print("-----------------------------------")
print("Comparing files ", " > " + fname1, " < " +fname2, sep=' ')
print("-----------------------------------")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
unequal_line_count=0
# 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:
unequal_line_count=unequal_line_count+1
if unequal_line_count <=2 :
#Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()
else :
print("NO")
# 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)
else :
f1_line = f1.readline()
f2_line = f2.readline()
print("YES")
#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()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.