windows - Unable to edit hosts file with python -


i trying write python script can block websites based on time of day not able edit hosts file in windows running cmd administrator. here's code:

import time datetime import datetime 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):     open(hosts_location,"r+") file:         content=file.read()         website in blocked_websites:             if website in content:                 pass             else:                 file.write(redirect+"\t"+website+"\n")     print("working hours....") else:     open(hosts_location,"r+") file:         content=file.readlines()         file.seek(0)         line in content:             if not any(website in line website in blocked_websites):                 file.write(line)         file.truncate()     print("fun time...") time.sleep(5) 

update:the code working fine local hosts file when try real hosts file administrator script flashes , cmd shows message "[errno 13]:permission denied". not able edit hosts notepad adminstrator os windows 10 home edition 64-bit.


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -