Advertisement
plarmi

workpython_14_1

Jul 9th, 2023
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.22 KB | None | 0 0
  1. def compare_files(file1_path, file2_path):
  2.     with open(file1_path, 'r') as file1, open(file2_path, 'r') as file2:
  3.         lines1 = file1.readlines()
  4.         lines2 = file2.readlines()
  5.        
  6.         min_len = min(len(lines1), len(lines2))
  7.        
  8.         for i in range(min_len):
  9.             if lines1[i] != lines2[i]:
  10.                 return lines1[i].strip(), lines2[i].strip()
  11.        
  12.         if len(lines1) > min_len:
  13.             return lines1[min_len].strip(), None
  14.         elif len(lines2) > min_len:
  15.             return None, lines2[min_len].strip()
  16.         else:
  17.             return None, None
  18.  
  19. # Пример использования
  20. file1_path = 'file1.txt'  # Путь к первому файлу
  21. file2_path = 'file2.txt'  # Путь ко второму файлу
  22.  
  23. mismatch1, mismatch2 = compare_files(file1_path, file2_path)
  24.  
  25. if mismatch1 is None and mismatch2 is None:
  26.     print("Файлы совпадают")
  27. else:
  28.     if mismatch1 is not None:
  29.         print(f"Первая несовпадающая строка в первом файле: {mismatch1}")
  30.     if mismatch2 is not None:
  31.         print(f"Первая несовпадающая строка во втором файле: {mismatch2}")
  32.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement