0

I created a program which modifies the hosts file in order to block some websites. But when I am running the programme, I am getting this error. Btw I have created my Python file using Pycharm IDE and my intention is to run this script everytime open my PC using Task Scheduler. So please kindly do tell me what I should be running as the administrator. Is it like the Pycharm itself? Most importantly how do I give it Admin permissions permanently?

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

Please kindly tell me a way to fix this.

Hrishi
  • 41
  • 9
  • 1
    Can the Python script be run as admin? – S3DEV Aug 02 '21 at 09:40
  • 2
    A regular user doesn't have write permission to that file. You'll need to run your program as an administrator. – AKX Aug 02 '21 at 09:41
  • 1
    See https://stackoverflow.com/q/29770455/10230104 – nickh Aug 02 '21 at 09:41
  • PyCharm is only IDE - it means editor with extra functions to develop code - and it has nothing to do with problem. After creating script you should run it without `PyCharm`. First you should check file's attributes - in console was function `attrib` for this. As I remeber long time ago it was in read-only mode and it needed only to remove this attribute. – furas Aug 02 '21 at 10:17

1 Answers1

2

On Windows create a scheduled task on logon, and configure the task to run under a system account. The task could simply be run as a batch file and within the batch file you may run the below command. Ensure the task is set to run with administrator privileges in the task creation window

C:\PythonFolder\python.exe yourscript.py
Vishal Bakshi
  • 101
  • 1
  • 7