PermissionError: [Errno 13] Permission denied: 'C:/Windows/System32/drivers/etc/host please help to solve this erorr

Viewed 41
  1. This is a python code for website blocking I am using jupyter notebook for run this code when i run this program I am getting error name as PermissionError
import datetime
    import time
    end_time=datetime.datetime(2022,9,22)
    site_block=["www.wscubetech.com","www.facebook.com"]
    host_path="C:/Windows/System32/drivers/etc/hosts"
    redirect="127.0.0.1"
    while True:
        if datetime.datetime.now()<end_time:
            print("Start Blocking..")
            ***with open(host_path,"r+") as host_file:***
                content = host_file.read()
                for website in site_block:
                    if website not in content:
                        host_file.write(redirect+" "+website+"\n")
                    else:
                        pass
        else:
             with open(host_path,"r+") as host_file:
                    content = host_file.readlines()
                    host_file.seek(0)
                    for lines in content:
                        if not any(website in lines for website in site_block):
                            host_file.write(lines)
                    host_file.truncate()
             time.sleep(5)

2.This is erorr we getting When I run this program PermissionError Traceback (most recent call last) Input In [15], in <cell line: 8>()

      9 if datetime.datetime.now()<end_time:
     10     print("Start Blocking..")
---> 11     with open(host_path,"r+") as host_file:
     12         content = host_file.read()
     13         for website in site_block:

PermissionError: [Errno 13] Permission denied: 'C:/Windows/System32/drivers/etc/hosts'

2 Answers

Permission denied simply means the system is not having permission to open the file to that folder.

C:\Windows\System32\drivers\etc\hosts is writable only by the Administrator. You should run your script in Administrator mode.

EDIT (23/09/2022 - comment):

I ran your code with pycharm in administrator mode, no error and no output but the file was modified (two extra lines then deleted):

I rewrote the code for testing. Here it is for a different approach:

site_block = ["www.facebook.com", "www.stackoverflow.com"]
host_path = "C:/Windows/System32/drivers/etc/hosts"
redirect = "127.0.0.1"
with open(host_path, "r+") as host_file:
    for website in filter(lambda website: website not in host_file.read(), site_block):
        host_file.write(redirect + " " + website + "\n")

time.sleep(5)

with open(host_path, "r") as host_file:
    lines = host_file.readlines()
    with open(host_path, "w") as output:
        for line in lines:
            if not any(redirect + " " + website + "\n" == line for website in site_block):
                output.write(line)

Tips:

  • You can use a boolean to know if you have already updated the file to avoid opening it every 5 seconds.
  • You can stop the process after cleaning the file.
  • You can also look to run the code with a cron.

The PermissionError is caused when you do not have a permission to do something. The file C:/Windows/System32/drivers/etc/hosts is protected by the permission. That is why you cannot access to it. Why don't you run it as administrator?

Related