chevron_left
660 points
9 3 2

Permissionerror: [errno 13] permission denied
Photo by James Wheeler

Permissionerror: [errno 13] permission denied

In this article, we will solve the problem `Permissionerror: [errno 13] permission denied` in the python program. This error occurs when reading a file, we can face a PermissionError if we specify a folder path rather than a file path or if Python does not have the necessary `permissions to open, read, or write files`. [Errno 13] error: permission denied. To solve this problem, we have to `remove those restrictions from that file`.


What is Permissionerror: [errno 13] permission denied error? #

When reading a file, we can face a PermissionError if we specify a folder path rather than a file path or if Python does not have the necessary permissions to open, read, or write files. [Errno 13] Error: Permission denied.

This issue generally occurs when we read, write, or rename files, among other file activities.

The permission denied error has three primary causes.

  • Insufficient privileges on the file or for Python
  • Passing a folder instead of a file
  • The file is already open by another process


How to fix this error? #

Let's try to reproduce the error 13 permission denied with the above scenarios and then look at some examples of how to remedy them.


Case 1: Insufficient privileges on the file or for Python #

Consider a local CSV file that contains private information that has to be safeguarded. You can change the file permissions to make sure that only you can read it.

Making a Python program to read the file and print its contents is the next step.

Program

# Python program to read the entire file (absolute path) using read() function
file = open("python.txt", "r")
content = file.read()
print(content)
file.close()

Output

Traceback (most recent call last):
  File "C:/Projects/Tryouts/python.txt", line 2, in <module>
    file = open("python.txt", "r")
PermissionError: [Errno 13] Permission denied: 'python.txt'

PermissionError is displayed when we run the code. [Errno 13] Because the file was created by the root user, permission was denied. The script is not being run in elevated mode (admin/root).

By launching the command prompt in administrator mode on Windows and running the appropriate Python script, we may resolve this mistake. Even if you receive the permission error winerror 5 access is denied error, the identical solution still works.

Using the sudo command, we may run the script as the root user in the Linux scenario.

You can also use the following command to examine the file permissions.

ls -la

# output
-rw-rw-rw-  1 root  srinivas  46 Jan  29 03:42 python.txt

Python cannot read the file in the example above because it is owned by the root user and we are not running Python as the root user.

By adjusting the permission to either a specific user or everyone, we can resolve the problem. Let's run the following command to make the file accessible to everyone and executable.

chmod 755 python.txt

As an alternative to making it readable to everyone, we can grant authorization to particular users. Executing the next command will allow us to do this.

chown srinivas:admin python.txt

You will see the output below when we run our code again after setting the appropriate permissions.

Dear User,

Welcome to Python Tutorial

Have a great learning !!!

Cheers


Case 2: Providing the file path #

When a legal file path is substituted for a folder path, as in the example below, the Python interpreter raises the Error 13 permission refused error.

Program

# Program to read the entire file (absolute path) using read() function
file = open("C:\\Projects\\Python\\Docs", "r")
content = file.read()
print(content)
file.close()

Output

Traceback (most recent call last):
  File "c:\Personal\IJS\Code\program.py", line 2, in <module>
    file = open("C:\\Projects\\Python\\Docs", "r")
PermissionError: [Errno 13] Permission denied: 'C:\\Projects\\Python\\Docs'

If we accept the file path dynamically, we can alter our code to check to see if the given file path is a legitimate file before processing it. This will allow us to correct the mistake.

Program

# Program to read the entire file (absolute path) using read() function
file = open("C:\\Projects\\Python\\Docs\python.txt", "r")
content = file.read()
print(content)
file.close()

Output

Dear User,

Welcome to Python Tutorial

Have a great learning !!!

Cheers


Case 3: Ensure the file is Closed #

When working with files in Python, we often forget to close them, leaving them open.

Because we did not close the file, we will receive a permission refused error the next time we attempt to access it because it is already in use by another process.

By making sure to close a file after executing an i/o operation on it, we may resolve this issue.


Conclusion

In this article, we solved the error `Permissionerror: [errno 13] permission denied` in python. This error occurred while reading a file, if we specify a folder path rather than a file path or if `Python does not have the necessary permissions to open, read, or write files`, we will get a PermissionError: [Errno 13] Error: Permission denied. We solved this error by taking three different cases.

If you read this far, tweet to the author to show them you care. Tweet a Thanks

More Posts

TypeError: 'int' object is not iterable in Python Ferdy - Sep 30
Typeerror: string indices must be integers Ankur Ranpariya - Sep 27
MySQL will not start without the configured ports free. Bushra Rubab - Sep 26
Python selenium wait for page to load. Bushra Rubab - Sep 26
Importerror: attempted relative import with no known parent package Ankur Ranpariya - Sep 26
Typeerror: module object is not callable Ankur Ranpariya - Sep 24
Cannot Unpack Non-Iterable Int Object zhteja - Sep 23
Importerror: cannot import name 'url' from 'django.conf.urls' NoirHusky - Sep 22
Importerror: cannot import name 'markup' from 'jinja2' zhteja - Sep 22
How many values can a function return. Bushra Rubab - Sep 22
The truth value of an array with more than one element is ambiguous. use a.any() or a.all() AAbouelenien - Sep 22
Find index of object in array in Javascript Baribor Saturday - Sep 21
Attributeerror: 'str' object has no attribute 'strftime' beekip - Sep 20
Write a Program to Print First x Terms of the Series 3n+2 which are not Multiples of 4 beekip - Sep 20
Indexerror: single positional indexer is out-of-bounds Ankur Ranpariya - Sep 20
Indentationerror: unindent does not match any outer indentation level Ankur Ranpariya - Sep 19
Dataframe' object has no attribute 'sort' AAbouelenien - Sep 17
Valueerror: setting an array element with a sequence Ankur Ranpariya - Sep 17
TypeError: a bytes-like object is required, not 'Problem' beekip - Sep 17
FileNotFoundError: [WinError 2] The system cannot find the file specified Ferdy - Sep 16