Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def compare_files(file1_path, file2_path):
- with open(file1_path, 'r') as file1, open(file2_path, 'r') as file2:
- lines1 = file1.readlines()
- lines2 = file2.readlines()
- min_len = min(len(lines1), len(lines2))
- for i in range(min_len):
- if lines1[i] != lines2[i]:
- return lines1[i].strip(), lines2[i].strip()
- if len(lines1) > min_len:
- return lines1[min_len].strip(), None
- elif len(lines2) > min_len:
- return None, lines2[min_len].strip()
- else:
- return None, None
- # Пример использования
- file1_path = 'file1.txt' # Путь к первому файлу
- file2_path = 'file2.txt' # Путь ко второму файлу
- mismatch1, mismatch2 = compare_files(file1_path, file2_path)
- if mismatch1 is None and mismatch2 is None:
- print("Файлы совпадают")
- else:
- if mismatch1 is not None:
- print(f"Первая несовпадающая строка в первом файле: {mismatch1}")
- if mismatch2 is not None:
- print(f"Первая несовпадающая строка во втором файле: {mismatch2}")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement