Notepad++ script permission denied to text file - python

I'm trying to scrape some information and then add it to a text file but when I run the script, I get the following error:
PermissionError: [Errno 13] Permission denied: 'stocks.txt'
I'm doing this for adding to the file.
f = open("stocks.txt", "a")
f.write(containers[0].string + "\n")
f.close()
When I go to my file explorer and right click the program and press run, it prints to the file but any other way gives me the error.

Related

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

This is Python code for website blocking. I am using Jupyter notebook to 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)
This is the error I get 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
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?

Opening Test File on Cisco IOSXE python guestshell

I successfully transferred a text file (zero_conf.txt) to a Cisco IOSXE (Amsterdam) switch flash file system using python on the guestshell.
However when I try to open the text file using the following code:
with open('/flash/zero_conf.txt','r+') as f:
lines = f.readlines()
I am getting the following error:
with open('/flash/zero_conf.txt','r+') as f:
FileNotFoundError: [Errno 2] No such file or directory: '/flash/zero_conf.txt'
Please let me know if I am making a syntax error or what will be the correct way to open a text file on the Cisco iosxe python guestshell.

Why I get PermissionError if file is closed? (Python)

I get this error while I'm running my code:
PermissionError: [WinError 32] The process cannot access the file because it is being used by another process: error
but my file is closed, can you please help?
def delete_employee(self, new_employee):
employees_csv_data = self.open_and_read_file(self.list_of_employees)
with open(EMPLOYEES_EDIT_FILE, 'w') as updated_csv:
writer = csv.writer(updated_csv)
if str(new_employee.employee_id) not in str(employees_csv_data.values):
print(EMPLOYEE_DOESNT_EXIST_IN_FILE_MSG)
else:
for row in employees_csv_data.values:
if new_employee.employee_id != str(row[0]):
writer.writerow(row)
os.rename(EMPLOYEES_EDIT_FILE, self.list_of_employees)
os.remove(self.list_of_employees)
Looks like some other application is accessing this file. Try to delete it and create it again or restart the system.

Getting permission error when editing hosts file

Using python, I am trying to edit the hosts file.
with open('C:\Windows\System32\drivers\etc\hosts', 'r+') as file:
data = file.readlines()
data[70] = '127.0.0.1 web.alanmrsa.com'
file.writelines(data)
print('done')
When I run this file, it gives me the following error:
PermissionError: [Errno 13] in python
C:\Windows\System32\drivers\etc\hosts is writable only by Administrator. You should run your script as Administrator instead.
Also note that you should do a file.seek(0) after data = file.readlines() so that you can overwrite the original content, and also do a file.truncate() after file.writelines(data) so that there would be no leftover characters from the original content in case your replacement string is shorter than the content of the original 71th line.

Access html file in write mode using python

example.py
file = open("innovative.html", "w")
file.write("This is a test\n")
file.write("And here is another line\n")
file.close()
return render_template('innovative.html')
while I am executing this file in server it throwing [Errno 13] Permission denied: 'innovative.html'
I am executing this file in rhcloud.
I changed permissions for file as -rwx- for innovative.html
Thanks in advance.
This error can occur when a file is actively opened or utilized by another process at the same time (as well as when permissions are not given). Be sure to check that the file is not being used while attempting to write.

Categories