PermissionError: [Errno 13] in Python

Just starting to learn some Python and I’m having an issue as stated below:

a_file = open('E:Python Win7-64-AMD 3.3Test', encoding='utf-8')

Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
    a_file = open('E:Python Win7-64-AMD 3.3Test', 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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When doing;

a_file = open('E:Python Win7-64-AMD 3.3Test', 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.3Testa.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.3Testa.txt', encoding='utf-8')

Method 2

For me, I was writing to a file that is opened in Excel.

Method 3

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)

Method 4

I encountered this problem when I accidentally tried running my python module through the command prompt while my working directory was C:WindowsSystem32 instead of the usual directory from which I run my python module


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x