summaryrefslogtreecommitdiff
path: root/day1/task4/task4.py
blob: 2583cb4b7a1117a844baa31227455511cc666e5e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
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'<BODY>(.|\n)*?</BODY>',
        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()