diff options
Diffstat (limited to 'day1/task2')
| -rw-r--r-- | day1/task2/task2.py | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/day1/task2/task2.py b/day1/task2/task2.py index 5a942dd..593df5b 100644 --- a/day1/task2/task2.py +++ b/day1/task2/task2.py @@ -1,30 +1,36 @@ import os +CHUNK_SIZE = 1024 + write_path = os.path.join(os.getcwd(), 'task2_data_1.txt') read_path = os.path.join(os.getcwd(), 'task2_data_2.txt') +try: + write_file = open(write_path, 'a') + read_file = open(read_path) + +except FileNotFoundError as e: + exception_path = str(e).strip('[Errno 2] No such file or directory:')[1:-1] + print(f'"{exception_path}" не существует') -if not os.access(write_path, os.F_OK): - print('Файла "{}" не существует'.format(write_path)) -elif not os.access(read_path, os.F_OK): - print('Файла "{}" не существует'.format(read_path)) +except PermissionError as e: + exception_path = str(e).strip('[Errno 13] Permission denied:')[1:-1] + print(f'Файл "{exception_path}" не доступен для чтения/записи') -elif not os.access(write_path, os.W_OK): - print('Файл "{}" не доступен для записи'.format(write_path)) -elif not os.access(read_path, os.R_OK): - print('Файл "{}" не доступен для чтения'.format(read_path)) +except IsADirectoryError as e: + exception_path = str(e).strip('[Errno 21] Is a directory:')[1:-1] + print(f'"{exception_path}" является директорией') else: - with open(write_path, 'a') as first, open(read_path) as second: - first.write('\n\n\n') + write_file.write('\n\n\n') - data = second.read(1024) - first.write(data) - while data: - data = second.read(1024) - first.write(data) + data = read_file.read(CHUNK_SIZE) + write_file.write(data) + while data: + data = read_file.read(CHUNK_SIZE) + write_file.write(data) + # пожалуйста не мучайте меня я не хочу писать еще три expept'a os.rename('task2_data_1.txt', 'task2_data_all.txt') os.remove('task2_data_2.txt') - |