# -*- coding: utf-8 -*-
"""
"""
def compare_lines_in_files(file1_path, file2_path):
try:
with open(file1_path, 'r', encoding='utf-8') as file1, open(file2_path, 'r', encoding='utf-8') as file2:
lines_file1 = file1.readlines()
lines_file2 = file2.readlines()
mismatched_lines = []
# Compare each line in file1 to all lines in file2
for line_num, line1 in enumerate(lines_file1, start=1):
line1 = line1.strip() # Remove leading/trailing whitespace
found_match = False
for line_num2, line2 in enumerate(lines_file2, start=1):
line2 = line2.strip() # Remove leading/trailing whitespace
# Perform a case-insensitive comparison
if line1.lower() == line2.lower():
found_match = True
break
if not found_match:
mismatched_lines.append(f"Line {line_num} in File 1: '{line1}' has no match in File 2")
# Compare each line in file2 to all lines in file1 (vice versa)
for line_num2, line2 in enumerate(lines_file2, start=1):
line2 = line2.strip() # Remove leading/trailing whitespace
found_match = False
for line_num, line1 in enumerate(lines_file1, start=1):
line1 = line1.strip() # Remove leading/trailing whitespace
# Perform a case-insensitive comparison
if line2.lower() == line1.lower():
found_match = True
break
if not found_match:
mismatched_lines.append(f"Line {line_num2} in File 2: '{line2}' has no match in File 1")
return mismatched_lines
except FileNotFoundError:
print("One or both files not found.")
return []
# Paths to the two text files you want to compare
file1_path = r'C:\Python Space\T1.txt'
file2_path = r'C:\Python Space\T2.txt'
mismatched_lines = compare_lines_in_files(file1_path, file2_path)
if mismatched_lines:
print("Differences between the files:")
for line in mismatched_lines:
print(line)
else:
print("No differences found between the files.")