From 8099312d4937b2d58ae3a4da40641190ace42505 Mon Sep 17 00:00:00 2001 From: pajjilykk Date: Wed, 25 Mar 2026 12:25:59 +0700 Subject: [PATCH] add lab2.py --- lab2.py | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 lab2.py diff --git a/lab2.py b/lab2.py new file mode 100644 index 0000000..69fbe67 --- /dev/null +++ b/lab2.py @@ -0,0 +1,72 @@ +def process_file(input_file, output_file): + try: + # Открытие входного файла + try: + with open(input_file, 'r', encoding='utf-8') as f: + text = f.read() + except FileNotFoundError: + print(f"[ОШИБКА] Файл '{input_file}' не найден.") + return + except PermissionError: + print(f"[ОШИБКА] Нет прав доступа к файлу '{input_file}'.") + return + except UnicodeDecodeError: + print(f"[ОШИБКА] Ошибка кодировки при чтении файла '{input_file}'. Попробуй другую кодировку.") + return + except Exception as e: + print(f"[ОШИБКА] Неизвестная ошибка при чтении файла: {e}") + return + + # Обработка текста + try: + result = "" + for char in text: + if ord(char) < 48: + result += " " + else: + result += char + except Exception as e: + print(f"[ОШИБКА] Ошибка при обработке текста: {e}") + return + + # Запись в выходной файл + try: + with open(output_file, 'w', encoding='utf-8') as f: + f.write(result) + except PermissionError: + print(f"[ОШИБКА] Нет прав на запись в файл '{output_file}'.") + return + except Exception as e: + print(f"[ОШИБКА] Ошибка при записи файла: {e}") + return + + print(f"[УСПЕХ] Обработка завершена. Результат записан в '{output_file}'.") + + except Exception as e: + print(f"[КРИТИЧЕСКАЯ ОШИБКА] {e}") + + +# --- Точка входа --- +def main(): + try: + input_file = input("Введите имя входного файла (например input.txt): ").strip() + output_file = input("Введите имя выходного файла (например output.txt): ").strip() + + if not input_file: + print("[ОШИБКА] Имя входного файла не может быть пустым.") + return + + if not output_file: + print("[ОШИБКА] Имя выходного файла не может быть пустым.") + return + + process_file(input_file, output_file) + + except KeyboardInterrupt: + print("\n[ОСТАНОВЛЕНО] Программа прервана пользователем.") + except Exception as e: + print(f"[ОШИБКА] Неожиданная ошибка: {e}") + + +if __name__ == "__main__": + main() \ No newline at end of file