Задачи планировщика задач - PullRequest
0 голосов
/ 02 ноября 2019

Вот код моего файла 'pyw'. В случае запуска его вручную, он работает отлично, но когда я пытаюсь настроить его с помощью планировщика задач, он не работает даже с некоторыми локальными файлами, не говоря об оригинальном файле 'hosts'.

ОС: Windows10 ver.1903

Интерпретатор: Python 3.7

Антивирус позволяет фиксировать изменения в файле 'hosts', поэтому это не так

import time
from datetime import datetime as dt

# Location of 'hosts' file and redirect address
host_path = "C:\\Windows\\System32\\drivers\\etc\\hosts"
redirect = "127.0.0.1"

begin = 8
end = 18
website_list = ['www.facebook.com', 'facebook.com', 'www.brazzers.com', 'brazzers.com', 'www.bing.com']


def execute():  # Executable 'pyw' code
    while True:
        if dt(dt.now().year, dt.now().month, dt.now().day, begin) < dt.now() \
                < dt(dt.now().year, dt.now().month, dt.now().day, end):
            with open(host_path, "r+") as file:
                content = file.read()
                for website in website_list:
                    if website in content:
                        pass
                    else:
                        file.write(redirect + " " + website + "\n")
        else:
            # Re-write file without addresses
            with open(host_path, "r+") as file:
                content = file.readlines()
                file.seek(0)
                for line in content:
                    # If there are no addresses in line then write above the existing text
                    if not any(website in line for website in website_list):
                        file.write(line)
                file.truncate()  # Delete old text in file
        time.sleep(60)


execute()
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...