Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def find_common_characters(input_files, output_file):
- common_characters = set()
- # Начальное заполнение множества common_characters символами из первого файла
- with open(input_files[0], 'r') as first_file:
- common_characters.update(set(first_file.read()))
- # Итерация по остальным файлам и постепенное сужение множества common_characters
- for file_name in input_files[1:]:
- with open(file_name, 'r') as input_file:
- file_characters = set(input_file.read())
- common_characters.intersection_update(file_characters)
- # Запись общих символов в итоговый файл
- with open(output_file, 'w') as output:
- output.write(''.join(sorted(common_characters)))
- # Пример использования
- input_files = []
- while True:
- file_name = input("Введите название файла (или 'quit' для завершения): ")
- if file_name == 'quit':
- break
- input_files.append(file_name)
- output_file = 'common_characters.txt' # Имя файла, в который будут записаны общие символы
- find_common_characters(input_files, output_file)
- print(f"Общие символы из файлов успешно записаны в файл '{output_file}'.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement