0

So I have a .pyw file, default program to open it is pythonw.exe, but instead when I run it it opens visual studio. Maybe a behind the scenes error? Program was set up in the task scheduler with highest permissions to access hosts file in windows. The test path works fine.

import time
from datetime import datetime as dt



# Temporary hosts path to test
hosts_temp = r"C:\Users\letto4135\Desktop\python_programs\web_block\hosts"

# Hosts path in windows
hosts_path = r'C:\Windows\System32\drivers\etc\hosts'

# Redirect to this IP
redirect = "127.0.0.1"

# List of Websites to block
website_list = ['www.facebook.com','facebook.com']



while True:
    # If between time, block sites
    if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() < dt(dt.now().year, dt.now().month,dt.now().day,16):
        with open(hosts_path,'r+') as file:
            content = file.read()
            for site in website_list:
                if site in content:
                    pass
                else:
                    file.write('\n' + redirect + ' ' + site)
    # If not between time allow sites
    else:
        with open(hosts_path, 'r+') as file:
            content = file.readlines()
            file.seek(0)
            for line in content:
                if not any (website in line for website in website_list):
                    file.write(line)
            file.truncate()
    # Run every ("") seconds.
    time.sleep(5)
letto4135
  • 23
  • 8

1 Answers1

0

The problem was in the Task scheduler. If this ever happens to you, under actions tab in program/script box put full path to pythonw.exe, then in Add arguments box put the full path to your program.

letto4135
  • 23
  • 8