Fixing Python PermissionError Errno 13 Permission denied

PermissionError: [Errno 13] Permission denied in Python is a very common error that occurs in Python when you try to open a file but the file path is actually a folder.

In this article I am going to explain why this error happens, how you can solve it or even avoid it in the first place, there are also other possible solutions that worked for other developers that I think may work for you in case my solution did not work or you did not like my solution.

Explaining : Python PermissionError Errno 13 Permission denied

Let us explain the problem, the problem is actually very simple, the error is caused when you try to open a file, but the path is actually a folder.

The error message will look like this.

Fixing Python PermissionError Errno 13 Permission denied-1

Sometimes the error can occur by mistake, you need to avoid the error, the solution that I liked is solution 1, other solution worked for other people depending on each individual case.

Solution 1 : my main fix

To avoid the problem that I detailed above we should run the following code, tweak your code accordingly.

Fixing Python PermissionError Errno 13 Permission denied-2

I would say the solution I described above should fix the issue for most people, if it does not please try to follow the solutions bellow, maybe one will do the job.

Solution 2 : change the permissions of the directory you want to save

If the other solution does not work it is hard for me to say this is going to work 100 percent, some developers propose changing the permissions of the directory you want to save.

The goal is to give all users the right to write and read.

You can do that manually on windows, there are a lot of tutorials online, I recommend the ones with step by step pictures.

Solution 3 : close the file you are trying to write before you save

The third solution may sound silly, but in coding we always make silly mistakes, silly solution do work sometimes.

You need to close the file you are trying to write before you save.

Try this and see what happens, if you found this article of value to you so far, please consider donating to our Kofi account, you can use the red button at the top of this page, the blog is free, you do not have to actually donate.

Solution 4 : change the permissions of the directory

The solution 4 is a practical solution to change the permissions, please understand the issue and follow the next step.

You can run CMD as Administrator and change the permission of the directory using cacls.exe. For example:

First run CMD as admin.

Then run the following line of code in order to change the permissions of the directory, in this code we are using cacls.exe a known command line tool.

Fixing Python PermissionError Errno 13 Permission denied-3

Summing-up

This is one of those errors that are hard to explain or spot, sometimes the error PermissionError: [Errno 13] Permission denied in Python comes with other problems with your code or Python which makes solving the issue even more complicated, I hope my article helped you solve this common issue, keep coding and learning, cheers.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/