Advertisement
plarmi

workpython_14_10

Jul 9th, 2023
681
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.40 KB | None | 0 0
  1. def find_common_characters(input_files, output_file):
  2.     common_characters = set()
  3.  
  4.     # Начальное заполнение множества common_characters символами из первого файла
  5.     with open(input_files[0], 'r') as first_file:
  6.         common_characters.update(set(first_file.read()))
  7.  
  8.     # Итерация по остальным файлам и постепенное сужение множества common_characters
  9.     for file_name in input_files[1:]:
  10.         with open(file_name, 'r') as input_file:
  11.             file_characters = set(input_file.read())
  12.             common_characters.intersection_update(file_characters)
  13.  
  14.     # Запись общих символов в итоговый файл
  15.     with open(output_file, 'w') as output:
  16.         output.write(''.join(sorted(common_characters)))
  17.  
  18. # Пример использования
  19. input_files = []
  20. while True:
  21.     file_name = input("Введите название файла (или 'quit' для завершения): ")
  22.     if file_name == 'quit':
  23.         break
  24.     input_files.append(file_name)
  25.  
  26. output_file = 'common_characters.txt'  # Имя файла, в который будут записаны общие символы
  27. find_common_characters(input_files, output_file)
  28.  
  29. print(f"Общие символы из файлов успешно записаны в файл '{output_file}'.")
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement