IOError: [Errno 13] Permission denied - python

I have this piece of code to create a .json file to store python data. When i run it in my server i get this error:
IOError: [Errno 13] Permission denied: 'juliodantas2015.json' at line with open(output_file, 'wb') as fp:
Python code:
fich_input='juliodantas2015.txt'
output_file= fich_input.strip('.txt')+'.json'
import json
with open(output_file, 'wb') as fp:
json.dump('yes', fp)
In command line i typed chmod 777 *.py but still not working. How can i fix this ?

I had a similar problem. I was attempting to have a file written every time a user visits a website.
The problem ended up being twofold.
1: the permissions were not set correctly
2: I attempted to use
f = open(r"newfile.txt","w+") (Wrong)
After changing the file to 777 (all users can read/write)
chmod 777 /var/www/path/to/file
and changing the path to an absolute path, my problem was solved
f = open(r"/var/www/path/to/file/newfile.txt","w+") (Right)

IOError: [Errno 13] Permission denied: 'juliodantas2015.json'
tells you everything you need to know: though you successfully made your python program executable with your chmod, python can't open that juliodantas2015.json' file for writing. You probably don't have the rights to create new files in the folder you're currently in.

I have a really stupid use case for why I got this error. Originally I was printing my data > file.txt
Then I changed my mind, and decided to use open("file.txt", "w") instead. But when I called python, I left > file.txt .....

I faced same issue this morning when I tried to write data to opened excel file note that you can not edit a file when it's open .
Please close the file and then it work normally

Related

Permission denied error while merging csv files [duplicate]

This question already has answers here:
PermissionError: [Errno 13] Permission denied
(16 answers)
Closed 6 months ago.
Just starting to learn some Python and I'm having an issue as stated below:
a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
Traceback (most recent call last):
File "<pyshell#9>", line 1, in <module>
a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
PermissionError: [Errno 13] Permission denied: 'E:\\Python Win7-64-AMD 3.3\\Test\
Seems to be a file permission error, if any one can shine some light it would be greatly appreciated.
NOTE: not sure how Python and Windows files work but I'm logged in to Windows as Admin and the folder has admin permissions.
I have tried changing .exe properties to run as Admin.
When doing;
a_file = open('E:\Python Win7-64-AMD 3.3\Test', encoding='utf-8')
...you're trying to open a directory as a file, which may (and on most non UNIX file systems will) fail.
Your other example though;
a_file = open('E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8')
should work well if you just have the permission on a.txt. You may want to use a raw (r-prefixed) string though, to make sure your path does not contain any escape characters like \n that will be translated to special characters.
a_file = open(r'E:\Python Win7-64-AMD 3.3\Test\a.txt', encoding='utf-8')
For me, I was writing to a file that is opened in Excel.
For me, I got this error when I was trying to write a file to a folder and wanted to make sure the folder existed. I accidentally used:
path = Path("path/to/my/file.txt")
path.mkdir(parents=True, exist_ok=True)
with open(path, "w") as file:
...
but the second line means "make a directory at this exact path (and make its parents too, without throwing errors for them existing already)". The third line then throws a PermissionError, because you can't use open() on a directory path, of course! The second line should have been:
path.parent.mkdir(parents=True, exist_ok=True)
I encountered this problem when I accidentally tried running my python module through the command prompt while my working directory was C:\Windows\System32 instead of the usual directory from which I run my python module

FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\SUN\\Desktop\\oops in python.txt'

I used a online compiler to access a document in my Desktop using python.
file = open(r"C:\Users\SUN\Desktop\oops in python.txt")
for i in file:
print(i)
file.close()
While, I run this code, I am getting this error
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\SUN\\Desktop\\oops in python.txt'
I Checked the file name and the directory. It's all correct. And I continue to get the same error.
Why am I getting this error?
When I run this code in a Offline Compiler, It works. And I Got the output as required.
It's because the online compiler can't access you system files. The website your are using creates a session for you and runs your code on their servers so you can only access the files you created and saved unto their server during your session.
I think the correct filename is "loops in python.txt" and not "oops in python.txt".
Aren't you missing the l in the beginning?
On windows, you can also use forward slashes (/).
with open ensures the file is closed automatically. i.e.:
with open("C:/Users/SUN/Desktop/loops in python.txt") as f:
for l in f:
...

PermissionError Errno 13 Permission denied

I am trying to read a directory which contains html files with python. The code I am using is this:
import os
f = open(r"C:\Users\Grty\Desktop\de", "w+")
for filename in os.listdir(os.getcwd()):
content = f.read()
print (filename, len(content))
The problem is I cant access the directory. I have tried different locations but the problem persists. I have also done the relative chmod 777 (Using windows 10) and still nothing. I enabled sharing with everyone, giving read/write permissions to everyone and also disabled the "read only" (which somehow is being re-enabled itself). I have also run the cmd as an admin and still no progress. Anyone got an idea of how to overcome this?
You are trying to open a folder for writing:
f = open(r"C:\Users\Grty\Desktop\de", "w+")
But this is a folder, which can't be opened using open() even in "r" mode, because it isn't a file, and if you try, Windows will say access denied. As you get each filename, open that:
for filename in os.listdir(os.getcwd()):
with open(filename) as f:
content = f.read()

Failing to open csv file

My current python script is like this:
import csv
with open ('2017_IL_sales.csv','r') as infile:
reader=csv.reader(infile)
with open('2017_IL_sales_report.csv') as outfile:
writer=csv.writer(outfile)
dict_report={rows[1]:rows[2] for rows in reader}
print dict_report
In brief, I want to open a csv file called 2017_IL_sales then create a dictionary for two columns inside. But with whatever reason, everytime I run the code via IDLE it told me this IOError: [Errno 2] No such file or directory: '2017_IL_sales.csv'. Anyone knows the reason?
Make sure that the script you are running is in the same folder as the file you are trying to read. If that is not possible, make sure to specify the correct file path.
Is IDLE's working directory that folder?
Run this in IDLE and see what you get:
import os
os.getcwd()

Windows 7, Python 3.6.0, can't make a new file due to "PermissionError".

So I used this code:
test_file = open('c:\\test.txt', 'w')
test_file.write('TEST')
test_file.close()
And it returned this error:
PermissionError: [Errno 13] Permission denied: 'c:\test.txt'
What was supposed to happen was that it would make a txt file, write TEST in it, and that's it.
Googling the error has returned no results. (that I could understand at any rate) I am quite new to programming and such, so please give me a simple answer (if possible).
import os
#you don't have permission to write to the root folder in C drive. You can try to write to your desktop
test_file = open(os.path.join(os.environ["HOMEPATH"], "Desktop","test.txt"), 'w')
test_file.write('TEST')
test_file.close()
#Now check your desktop you should see the test.txt file

Categories