Home > front end >  PermissionError: [Errno 13] Permission denied: 'C:/Windows/System32/drivers/etc/host please hel
PermissionError: [Errno 13] Permission denied: 'C:/Windows/System32/drivers/etc/host please hel

Time:09-23

  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'

CodePudding user response:

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.

CodePudding user response:

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