125k views
22 votes
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.

User Pauloya
by
4.4k points

1 Answer

12 votes

Answer:

first = input("enter first file name: ")

second = input("enter second file name: ")

file_one = open(first, 'r')

file_two = open(second, 'r')

if file_one.read() == file_two.read():

print("Both files are the same")

else:

print("Different files")

file_one.close()

file_two.close()

Step-by-step explanation:

The python module above is a procedural program. The first two lines ask for user inputs for both file names in string, then the files are opened and compared with the if-statement. At the end of the program, the files are closed to avoid leakage.