diff options
| author | Andrew <saintruler@gmail.com> | 2019-03-09 20:52:00 +0400 |
|---|---|---|
| committer | Andrew <saintruler@gmail.com> | 2019-03-09 20:52:00 +0400 |
| commit | 8d49433235daa0ca3e7ad1b330e4215ee86f4f51 (patch) | |
| tree | 8d34a93fcbbc6175f849f7f591acd0435cff21dc /day1/task5/task5.py | |
| parent | 82383dcaaf1fc3eebe6b6b27565106013ab7d851 (diff) | |
Исправлена пятая задача первого дня, работа с байтами изменена на работу с числами вместо строк.
Diffstat (limited to 'day1/task5/task5.py')
| -rw-r--r-- | day1/task5/task5.py | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/day1/task5/task5.py b/day1/task5/task5.py index 52eafc2..2869247 100644 --- a/day1/task5/task5.py +++ b/day1/task5/task5.py @@ -1,15 +1,20 @@ -with open('task5_data.txt') as f: - print(f.read()) - -with open('task5_data.txt', 'rb+') as input_byte, open('task5_data_new.txt', 'wb') as out: - char = input_byte.read(1) - while char: - binary = list(bin(int(char.hex(), 16))[2:]) - if binary[-2] == '1': - binary[-2] = '0' - elif binary[-2] == '0': - binary[-2] = '1' - - out.write(bytes([int(''.join(binary), 2)])) - char = input_byte.read(1) +import os + +read_path = os.path.join(os.getcwd(), 'task5_data.txt') +write_path = os.path.join(os.getcwd(), 'task5_data_new.txt') + +if not os.access(read_path, os.R_OK): + print('Файл "{}" не доступен для чтения'.format(read_path)) +elif not os.access(write_path, os.W_OK): + print('Файл "{}" не доступен для записи'.format(write_path)) +else: + with open(read_path) as f: + print(f.read()) + + with open('task5_data.txt', 'rb') as input_byte, open(write_path, 'wb') as out: + char = input_byte.read(1) + while char: + number = int(char.hex(), 16) + out.write(bytes([number ^ 2])) + char = input_byte.read(1) |