Table of Contents Hide
- What is permission denied error?
- Reasons of permissionerror ERRNO 13 in Python
Permission denied means you are not allowed to access a file. But why this happens? This is because a file has 3 access properties – read, write, and execute. And 3 sets of users – owner, group, and others. In this article we will look at different solutions to resolve PermissionError: [Errno 13] Permission denied.
What is permission denied error?
Suppose you have a file in your computer but you can’t open it. Strangely another user can open the same file. This is because you don’t have permission to read the content of that file.
Permission denied on files to protect them. For example, you have stored username & password for your database in a file so you never want it to be accessible to anyone except the database application and you. That’s why you will restrict it from other applications, software, processes, users, APIs etc.
In a system, root user can access everything. So, we seldom use the root account otherwise there is no meaning of security. A software running with root privilege can access your password file which you wanted to be secure. That’s why
sudo should be used with caution.
How these permissions are defined?
There are 3 types of permissions – read, write and execute. With read permission a software, process, application, user etc. can read a file. Similarly, with write permission they can write to the file. Execute is used to run it.
Now the question is, how to apply these permissions to different software, users etc.? Well there are 3 types of users – owner, group, and others. So, you can assign 3 sets of permissions, like this –
|Owner||An account of system who we want to separately assign permissions||r – read
w – write
x – execute
|Group||A set of multiple accounts, software, processes who can share the same permissions||r – read
w – write
x – execute
|Others||All the other entities of system||r – read
w – write
x – execute
Let’s get back to our password file example. Now you are into
others user category because you are not the owner of the file and probably not the part of group. People use to give least permissions to the
others because these are the access levels for everyone.
The password file won’t have any permission in
others category. And trying to access that will result in permission error: permission denied.
Reasons of permissionerror ERRNO 13 in Python
Primarily these are the reasons of permissionerror: errno13 permission denied in Python –
- Using folder path instead of file path while opening a file.
- Trying to write to a file which is a folder.
- Trying to write to a file which is already opened in an application.
- File permission not allowing python to access it.
- File is hidden with hidden attribute.
Let’s understand each of these reasons one by one and check their solutions.
Using folder path instead of file path while opening a file
To open a file for reading or writing, you need to provide the absolute or relative path to the file in
open function. Sometimes we create path of parent folder instead of file and that will lead to the permission error 13. Check out this code –
file = open("C:\\users\\akash\\Python\\Documents", "r") file.close()
The output will be –
Traceback (most recent call last): File "jdoodle.py", line 2, in <module> file = open("C:\\users\\akash\\Python\\Documents", "r") PermissionError: [Errno 13] Permission denied: 'C:\\users\\akash\\Python\\Documents'
The reason for the error in above code is that we have used
C:\users\akash\Python\Documents as path which is a directory. Instead we needed to use
Trying to write to a file which is a folder
This is a common case. Suppose you want to write to a file using Python but a folder with same name exists at that location then Python will get confused and try to write to a folder. This is not a right behavior because folders are not files and you can’t write anything on them. They are used for holding files only.
Trying to write to a file which is a folder will lead to permission error errno 13. Check this code –
# Directory Structure # 📂/ # |_ 📂Users # |_ 📁myFile file = open("/Users/myFile", "w") file.write("hello") file.close()
The output will be –
Traceback (most recent call last): File "jdoodle.py", line 2, in <module> file = open("/Users/myFile", "r") PermissionError: [Errno 13] Permission denied: '/Users/myFile'
In the above example we showed the directory structure and tried to write to
myFile is already a name of directory in the path. Generally, if a file doesn’t exist and we try to write to it then Python creates the file for us. But in this case there is already a directory with the provided name and Python will pick it. This will lead to permission error.
The solution to this problem is to either delete the conflicting directory or use a different file name.
Trying to write to a file which is already opened in an application
If a file is already opened in an application then a lock is added to it so that no other application an make changes to it. It depends on the applications whether they want to lock those files or not.
If you try to write to those files or more, replace, delete them then you will get permission denied error.
This is very common in PyInstaller if you open a command prompt or file explorer inside the dist folder, then try to rebuild your program. PyInstaller wants to replace the contents of dist but it’s already open in your prompt/explorer.
The solution to this problem is to close all the instances of applications where the file is opened.
File permission not allowing python to access it
In the above section we talked about file permissions. Python will be in others category if it is not set as user or in a group. If the file has restrictive permissions for others then Python won’t be able to access it.
Suppose the permission to the file is –
Owner – read, write, execute
Group – read, write
Others – none
Then only owner and group will be able to read and write to the file. Others will not be able to access it. Check out this image –
In this image the file owner is www, group is www, owner permissions are read+write, group permission is read only, while others have no permissions.
The solution to this problem is to either provide appropriate permission to the file for others, or add Python as owner or to the group.
Another solution is to run Python with root privilege.
File is hidden with hidden attribute
If your file is hidden then python will raise permission error. You can use subprocess.check_call() function to hide/unhide files. Check this code –
import subprocess myPath = "/Users/myFile.txt" subprocess.check_call(["attrib", "-H", myPath]) file_ = open(myPath, "w")
In the above code
-H attribute is used to unhide the file. You an use
+H to hide it again.
In this article we saw a number of reasons for Python to throw PermissionError: [Errno 13] Permission denied and discussed about their solutions with code examples. Follow the steps provided in article and you will be able to resolve this issue.