Python does not have permission to open .txt file - python

Disclaimer This question might be a duplicate of a question I could not find by searching.
I am trying to open a .txt file I downloaded. I used:
with open("spam.txt", "a+") as ef:
#other code that works
and I am gettingIOError [Errno 13] Permission denied: 'spam.txt'
Is there something I did wrong or do I have to open the file a different way?
Edit: I checked and I do have all the read, write, and execute permissions for the file and I can open it on Notepad. I am using Windows as my OS.

Well, I got the problem fixed. It turned out that Komodo Edit 9.3 needs to be in Admin Mode in order to do anything with files now.
The way to get past it is to run Komodo Edit as Admin or run it from a Python window.

I do not believe it is your code. Are you on a linux environment? If so, check to see if the file has read permissions.
The code works just fine.
with open("white-list.txt","a+") as ef:
print ef
<open file 'white-list.txt', mode 'a+' at 0x10206a6f0>
https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files

Related

No such file or directory error when trying to open a file executing a Python script via VSCodium

I've been using VSCodium lately and writing Python code with it. However, I can't seem to find a way to open an existing file when running the script with VSCodium.
This is the code I have, it's working perfectly fine when ran from a terminal, it works. But not with VSCodium.
def remove_e_words(path,path_copy):
newfile = open(path_copy,"w")
orig = open(path,"r")
lines = orig.readlines()
for line in lines:
newfile.write(sans_e_spec(line)+"\n")
newfile.close()
orig.close()
Here's the error I have :
FileNotFoundError: [Errno 2] No such file or directory: 'vers-queneau.txt'
I've searched for similar issues on stackoverflow, I've tried this:
Can't run python code through VS Code. Can't open file ptvsd_launcher.py [Errno 22] Invalid Argument
I have the downgraded version, it still doesn't work.
I also tried to use the file directory instead, changing a parameter in the Settings, it also didn't fix the issue.
What could I do to be able to solve this problem please?
Thanks.
You probably don't have the current working directory set to what you expect. If you run your code with print(os.getcwd()) you will very likely find that is not the directory you expect it to be. Either adjust your relative file path to take that cwd into account or specify the path in an absolute fashion (if the file is next to the file you're running you can calculate its location based on __file__).

Python 3.6 fails to open read-only file using "r+b" mode

In Python 3.6, when I attempt to "open('foobar', "r+b") with a file whose permissions are '-r--r--r--' (in CentOS7), I get a permission failure:
"builtins.PermissionError: [Errno 13] Permission denied: 'full/path/to/foobar'"
It opens just fine with "r", and the "r+b" works just fine if the permissions are '-rw-rw-rw-'.
I do need to open these files read-only, I'd like them to have read-only protections in the directory (so that they aren't inadvertently changed by other code), and I do need to read them as binaries. Is this a feature/bug of Python 3.6?
I'd like to know if I'm doing something incorrect, or if there's some work-around if not. I'd really like to avoid upgrading to 3.8 right now.
You are using the + mode, which is trying to open the file for update. Try without the + and it should work.
Per the help:
'+' open a disk file for updating (reading and writing)

Cannot update file menu Recent File list Your operating system says [Errrno 13] Premission denied: C:\\Users\\Eigenaar\\.idlerc\\recent-file.Ist

I want to make something in python, but this always appears
Cannot update file menu Recent File list Your operating system says
[Errrno 13] Premission denied:
C:\Users\Eigenaar\.idlerc\recent-file.Ist
Select OK and IDLE will continue without updating.
I read the answers here but nothing works!
I have the exact same problem. And, yes, it happens every time that I open IDLE.
I have Python 3.6.5 in a folder in Program Files, while the files that I have written are in a folder labeled Python under C:\Users\John\Documents.
I have a hunch that Python wants the files that I have written in a different location, perhaps under C:\Users\John.idlerc?
What do folks think?
JohnR
PS ~ I didn't see on this web page where to find the answers that others wrote to the original inquiry.

How can i open a folder using python?

I've tried reading through the other answered similar questions, yet still can't find what I'm doing wrong.
file=open("Crawl","r")
cont=file.read()
file.close
print(cont)
It's as simple as that. Both the folder and python are on the desktop which explains why there isn't a directory to it. I did try opening .txt and .png files and that did work, but it seems I can't open folders?
PermissionError: [Errno 13] Permission denied: 'Crawl'
You are getting this error because you are trying to open "Crawl" as a file. It's a folder, and you can't treat a Windows folder as if it were a file.

PermissionError: [Errno 13] when trying to run from NotePad++ but not from IDLE

I have the following simple code:
fh = open('example.txt','w')
fh.write('something')
fh.close()
If I use Python's IDLE, the scripts works as intended, it creates a file called example.txt in the same directory that the script is in.
But, if I do the same from within Notepad++, using the "Run..." command and the command line c:\python35\python.exe -i "$(FULL_CURRENT_PATH)", it opens the python window, opens the script file and gives a PermissionError: [Errno 13] Permission denied: 'example.txt'
I don't know what this thing is doing, I suspect that its trying to write the example.txt file inside the python's directory and not where the script is. So, I changed the command line in the Run command in Notepad++ to simply python -i "$(FULL_CURRENT_PATH)" (I have the python directory in the %PATH%), but it gives the same error.
I know this problem is more related to Notepad++ configuration than to a python problem, but any help will be appreciated.
I'm using Windows 10, python 3.5, Notepad++ 6.8.1
Thanks a lot.
The correct answer is what Juxhin said, Notepad++ must be run as administrator. I liked it was posted as an answer!!!

Categories