/
sejeenn
/
python_basic
Обзор
Документация
Войти
/
sejeenn
/
python_basic
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Module28/01_work_with_file_2/main.py
23 строки
904 B
Eugene Vorontsov
Copy repo python basic
03 фев 2025, 20:23
03 фев 2025, 20:23
a8fce4a
Код
Авторство
О чём код?
import os class File(object): """Модернизированная версия контекст-менеджера File""" def __init__(self, file_name: str, mode: str) -> None: if os.path.exists(file_name): self.file_object = open(file_name, mode, encoding='UTF-8') else: self.file_object = open(file_name, mode='w', encoding='UTF-8') self.file_object.write('Файла с именем {} не существует, ' 'поэтому создан новый файл с таким именем.'.format(file_name)) self.file_object = open(file_name, mode='r', encoding='UTF-8') def __enter__(self) -> object: return self.file_object def __exit__(self, exc_type, exc_val, exc_tb): self.file_object.close() with File('test.txt', 'r') as text: print(text.read())