Hello everyone.
I need a Python script that would check two text files, and if the text in the lines matches, then the script would write “Match found” and write the matching text to a new file. If there is no match, then it would write “No matches found.”
Can anyone help?
Something like this:
def check_files(file1, file2, output_file):
with open(file1, 'r') as f1, open(file2, 'r') as f2:
lines1 = f1.readlines()
lines2 = f2.readlines()
matches = [line for line in lines1 if line in lines2]
with open(output_file, 'w') as out:
if matches:
print("Match found")
for match in matches:
out.write(match)
else:
print("No matches found")
check_files('file1.txt', 'file2.txt', 'matches.txt')