import string import re import os html_path = os.path.join(os.getcwd(), 'task4_data.html') folder_path = '' # Не знаю зачем, но в задании так написано try: os.mkdir('../htmls') folder_path = '../htmls' except PermissionError: print('Директория на уровень выше не доступна для записи.') print('Создаем папку в текущей директории...') try: os.mkdir('htmls') folder_path = 'htmls' except PermissionError: print('Текущая директория не доступна для записи. Завершаем работу...') quit() try: html_file = open(html_path) os.chdir(folder_path) ascii_file = open('ascii_cleared.html', 'w', encoding='utf-8') body_file = open('body_cleared.html', 'w', encoding='utf-8') 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: data = html_file.read() cleared_data = ''.join(filter(lambda char: char in string.printable, data)) ascii_file.write(cleared_data) ################### body_cleared = re.sub( r'(.|\n)*?', lambda match: match.string[match.start(): match.end()].replace('\n', ''), data ) body_file.write(body_cleared) html_file.close() ascii_file.close() body_file.close()