/
stendal
/
Files
Обзор
Документация
Войти
/
stendal
/
Files
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
patch
task_3.py
50 строк
1 KB
stendal
upload files
04 июл 2025, 23:39
04 июл 2025, 23:39
aad57bd
Код
Авторство
О чём код?
import os def merge_files(input_files, output_file): """ Combines files by sorting them by the number of rows. Args: input_files (list): list of files to merge output_file (str): name of the resulting file Returns: str: success message """ files_info = [] # Checking the existence of files for file_name in input_files: if not os.path.exists(file_name): return f'Ошибка: файл {file_name} не найден' with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() files_info.append({ 'name': os.path.basename(file_name), 'line_count': len(lines), 'content': lines }) # Sorting files by number of lines files_info.sort(key=lambda x: x['line_count']) # Record result with open(output_file, 'w', encoding='utf-8') as f: for file_info in files_info: f.write(f"{file_info['name']}\n") f.write(f"{file_info['line_count']}\n") f.writelines(file_info['content']) f.write('\n') return f'Файлы успешно объединены в {output_file}' # Usage example if __name__ == "__main__": input_files = ['1.txt', '2.txt'] output_file = 'task_3.txt' result = merge_files(input_files, output_file) print(result)