@Mirozr

Python не видит директорию — что делать?

Код:
import time
from datetime import datetime

start_time = datetime(datetime.now().year, datetime.now().month, datetime.now().day, 9)
finish_time = datetime(datetime.now().year, datetime.now().month, datetime.now().day, 18)

print(start_time)
print(finish_time)

hosts = r'C:\Windows\System32\drivers\etc\hosts'
# hosts = '/etc/hosts'
redirect_url = '127.0.0.1'

blocked_sites = ['www.youtbe.com', 'youtube.com']

while True:
    if start_time < datetime.now() < finish_time:
        print('Доступ ограничен')

        with open(hosts, 'r+') as file:
            src = file.read()

            for site in blocked_sites:
                if site in src:
                    pass
                else:
                    file.write(f'{redirect_url} {site}\n')
    else:
        with open(hosts, 'r+') as file:
            src = file.readlines()
            file.seek(0)

            for line in src:
                if not any(site in line for site in blocked_sites):
                    file.write(line)
            file.truncate()
        print('Доступ открыт')

    time.sleep(5)


Ошибка:
Traceback (most recent call last):
  File "D:\IT\всі файли\SiyeHack.py", line 29, in <module>
    with open(hosts, 'r+') as file:
PermissionError: [Errno 13] Permission denied: 'C:\\Windows\\System32\\drivers\\etc\\hosts'
  • Вопрос задан
  • 219 просмотров
Решения вопроса 1
AlexNest
@AlexNest Куратор тега Python
Работаю с Python/Django
Все он видит. Прав у него на редактирование нет.
Hosts можно редактировать только с правами администратора.
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы