Question: Python Webblocker code I have this code for a web blocker in python, but it keeps getting this error and I don't know how to
Python Webblocker code I have this code for a web blocker in python, but it keeps getting this error and I don't know how to solve it. The error: C:\Users\ASUS\PycharmProjects\pythonProject\venv\Scripts\python.exe C:/Users/ASUS/PycharmProjects/pythonProject/rar.py Traceback (most recent call last): File "C:\Users\ASUS\PycharmProjects\pythonProject ar.py", line 35, inblock_websites(9, 18) File "C:\Users\ASUS\PycharmProjects\pythonProject ar.py", line 18, in block_websites with open(default_hoster, 'r+') as hostfile: TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper Do the work .... Process finished with exit code 1 I run it in pycharm. I have also put the websites in the hosts file already The code: import time from datetime import datetime as dt sites_to_block = [ 'www.face**ook.com', 'fa**book.com', 'www.you**tube.com', 'you**ube.com', 'www.gmai**l.com', 'gma**il.com' ] Window_host = open(r"C:\Windows\System32\drivers\etc\hosts.txt") default_hoster = Window_host redirect = "127.0.0.1" def block_websites(start_hour , end_hour): while True: if dt(dt.now().year, dt.now().month, dt.now().day,start_hour)< dt.now() < dt(dt.now().year, dt.now().month, dt.now().day,end_hour): print("Do the work ....") with open(default_hoster, 'r+') as hostfile: hosts = hostfile.read() for site in sites_to_block: if site not in hosts: hostfile.write(redirect+' '+site+' ') else: with open(default_hoster, 'r+') as hostfile: hosts = hostfile.readlines() hostfile.seek(0) for host in hosts: if not any(site in host for site in sites_to_block): hostfile.write(host) hostfile.truncate() print('Good Time') time.sleep(3) if __name__ == '__main__': block_websites(9, 18)
i put ** on the website list because of chegg, but those aren't the actual websites
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
