From 8916d790f44b95a60fd7ee3001c9e97b703f7ae0 Mon Sep 17 00:00:00 2001 From: Andrew Date: Mon, 11 Mar 2019 22:59:15 +0400 Subject: =?UTF-8?q?=D0=9E=D1=87=D0=B5=D1=80=D0=B5=D0=B4=D0=BD=D0=BE=D0=B5?= =?UTF-8?q?=20=D0=B8=D1=81=D0=BF=D1=80=D0=B0=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D0=BF=D0=B5=D1=80=D0=B2=D0=BE=D0=B9=20=D0=B7=D0=B0?= =?UTF-8?q?=D0=B4=D0=B0=D1=87=D0=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- day1/task1/task1.py | 40 +++++++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 11 deletions(-) diff --git a/day1/task1/task1.py b/day1/task1/task1.py index 61ab4cf..a763a97 100644 --- a/day1/task1/task1.py +++ b/day1/task1/task1.py @@ -1,19 +1,37 @@ import os +CHUNK_SIZE = 1024 + character = input('Введите один символ: ') -while len(character) > 1: +while len(character) != 1: character = input('Введите РОВНО один символ: ') path = os.path.join(os.getcwd(), 'task1_data.txt') -if not os.access(path, os.R_OK): - print('Файл "{}" не доступен для чтения'.format(path)) + + +try: + f = open(path) + +except FileNotFoundError as e: + exception_path = str(e).strip('[Errno 2] No such file or directory:')[1:-1] + print(f'"{exception_path}" не существует') + +except PermissionError as e: + exception_path = str(e).strip('[Errno 13] Permission denied:')[1:-1] + print(f'Файл "{exception_path}" не доступен для чтения/записи') + +except IsADirectoryError as e: + exception_path = str(e).strip('[Errno 21] Is a directory:')[1:-1] + print(f'"{exception_path}" является директорией') + else: - with open(path) as f: - data = f.read(1024) - i = data.count(character) - while data: - data = f.read(1024) - i += data.count(character) - - print('В данном файле {} символов "{}"'.format(i, character)) + data = f.read(CHUNK_SIZE) + i = data.count(character) + while data: + data = f.read(CHUNK_SIZE) + i += data.count(character) + + f.close() + + print(f'В данном файле {i} символов "{character}"') -- cgit v1.2.3