/
valek
/
homework-files
Обзор
Документация
Войти
/
valek
/
homework-files
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
task3.py
29 строк
1 KB
valek
update task3.py
20 ноя 2025, 11:43
20 ноя 2025, 11:43
58f7848
Код
Авторство
О чём код?
def combine_files(file_names, output_file): file_data = [] for file_name in file_names: with open(file_name, 'r') as f: lines = f.readlines() file_data.append((file_name, len(lines), lines)) return file_data.sort(key=lambda x: x[1]) with open(output_file, 'w') as outfile: for file_name, line_count, lines in file_data: outfile.write(f"{file_name}\n") outfile.write(f"{line_count}\n") outfile.writelines(lines) if __name__ == "__main__": file_names = ["1.txt", "2.txt"] output_file = "output.txt" with open("1.txt", "w", encoding="utf-8") as f: f.write("Духовной жаждою томим\n") f.write("В пустыне мрачной я влачился\n") with open("2.txt", "w", encoding="utf-8") as f: f.write("Восстань, пророк, и виждь, и внемли\n") combine_files(file_names, output_file) print(f"Combined files into {output_file}")