Question: Task 1 : Write a script named dif.py . This script should prompt the user for the names of two text files and compare the

Task 1: Write a script named
dif.py. This script 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 script should simply output "Yes". 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. (LO: 4.1,4.5)
My code:
def compare_files(file1, file2):
"""
Compare the contents of the two files.
"""
with open(file1,'r') as f1:
with open(file2,'r') as f2:
for line1, line2 in zip(f1, f2):
if line1!= line2:
print("No")
print(line1)
print(line2)
return
print("Yes")
def main():
"""
Test function
"""
file1= input("Enter the first file name: ")
file2= input("Enter the second file name: ")
compare_files(file1, file2)
if __name__=="__main__":
main()
exit(0)
Test Feedback:
Status: PASSED!
Check: 1
Test: Program outputs correct values for file1.txt and file2.txt
Reason: 'Yes' was found in the program's output. None
Timestamp: 2024-09-2304:24:35.403166
Status: FAILED!
Check: 2
Test: Program outputs correct values when user inputs 'goodbye'
Reason: Unable to find '['No and mismatched lines']' in the program's output.
Enter the name of the first file: Enter the name of the second file: No
hello from file1.txt
Max from file3.txt
Error : AssertionError -
Timestamp: 2024-09-2304:24:35.419460
Task 1 : Write a script named dif.py . This

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!