2

I am trying to write a python script which can block websites based on the time of the day but i am not able to edit the hosts file in windows even running the cmd as an administrator. Here's the Code:

import time
from datetime import datetime as dt 
hosts_location=r"C:\Windows\System32\drivers\etc\hosts"
hosts_temp="hosts"
blocked_websites=['www.facebook.com','facebook.com']
redirect="127.0.0.1"

while True:
   if dt(dt.now().year,dt.now().month,dt.now().day,8) < dt.now() < 
   dt(dt.now().year,dt.now().month,dt.now().day,17):
    with open(hosts_location,"r+") as file:
        content=file.read()
        for website in blocked_websites:
            if website in content:
                pass
            else:
                file.write(redirect+"\t"+website+"\n")
    print("Working Hours....")
else:
    with open(hosts_location,"r+") as file:
        content=file.readlines()
        file.seek(0)
        for line in content:
            if not any(website in line for website in blocked_websites):
                file.write(line)
        file.truncate()
    print("Fun Time...")
time.sleep(5)

Update:The code is working fine for a local hosts file but when i try it for the real hosts file even as administrator the script just flashes and with cmd shows message "[Errno 13]:Permission Denied". Also i am not able to edit hosts even with notepad even as adminstrator my os is windows 10 home edition 64-bit.

5
  • 4
    What is the error you get? Aug 18, 2017 at 14:14
  • And what's with all the dt.now().something calls? Aug 18, 2017 at 14:15
  • 2
    Your indentation is off as well. After while True: everything should be indented it's possible that's what's causing your problem
    – bendl
    Aug 18, 2017 at 14:16
  • I didn't run this exact code (because I don't want my hosts file to be completely destroyed), but I can't reproduce your problem. Maybe you should elaborate on why you think you can't modify the file (Does anything throw an exception?).
    – Aran-Fey
    Aug 18, 2017 at 14:27
  • 1
    I get this error even running cmd as administrator: PermissionError: [Errno 13] Permission denied: 'C:\\Windows\\System32\\drivers\\etc\\hosts' Aug 20, 2017 at 5:23

5 Answers 5

1

First of all, If you are not an administrator, change permissions of the hosts file by right clicking--> Properties --> Security and allow Read/Write/Execute Permissions for Users. Then open Command Prompt as administrator and run the script. It will work!

0

try to save the python file as a .pyw extension This will work for you.

0

Go to the hosts file and uncheck "read only" then go to security tab and give the permissions to user to read and write. Hope it'll work.

0

You should change access to file from users. Go to C:\Windows\System32\drivers\etc and click right on properties, choose security and edit access to file.

enter image description here

-1

Just change the path to be:

hosts_location = "C:\\Windows\\System32\\drivers\\etc\\hosts"

without r", and it will work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.