72 lines
2.8 KiB
Python
72 lines
2.8 KiB
Python
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() |