close

[Solved] PermissionError: [Errno 13] in Python

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error PermissionError: [Errno 13] in Python in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How PermissionError: [Errno 13] in Python Error Occurs?

Today I get the following error PermissionError: [Errno 13] in Python in python.

How To Solve PermissionError: [Errno 13] in Python Error ?

  1. How To Solve PermissionError: [Errno 13] in Python Error ?

    To Solve PermissionError: [Errno 13] in Python Error 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:

  2. PermissionError: [Errno 13] in Python

    To Solve PermissionError: [Errno 13] in Python Error 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:

Solution 1

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')

Solution 2


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)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read