Fix error

Fix – PermissionError: [Errno 13] Permission denied in Python

Fix Pandas concat InvalidIndexError

The python error “PermissionError: [Errno 13] Permission denied” can be fixed in various ways after knowing the root cause. This error mainly occurs through the open method which is used to open and read file. For example, if the path passed into the open() method as an argument is not a file, python will throw an error. Also, if you passed in a folder path instead of a file path. The solution to this approach is to apply how programmers think and check first if the path is a file with the os.path.isfile() method before you open.

import os

path = r"/path/to/filename.txt"
// check if the path is a file to avoid error
assert os.path.isfile(path)
with open(path, "r") as f:
    pass

The above code will help us to know if the path called in the open method is really a file. If is not, because we’re checking we will be able to perform another operation. That’s how you fix the python error permissionError: [Errno 13] Permission denied if the bug is related to the file path.

Another cause of the above error is related to the operating system and python. It could be that there is a restriction on the directory where you want to perform the file operation. If that is the case, then python will not have the required permission to open, read or write to a file and if you try, it will throw the error PermissionError: [errno 13] permission denied.

NB: The PermissionError: [errno 13] permission denied error occurs when you try to access a file from Python without having the required permissions to perform file operation.

import csv

with open("employees.csv", "r") as file:
	reader = csv.reader(file)
	for r in reader:
		print(r)

If we run the above python application, because we don’t have the necessary permission to read the file, it will throw an error with a stack-trace.

Traceback (most recent call last):
  File "app.py", line 3, in <module>
	with open("employees.csv", "r") as file:
PermissionError: [Errno 13] Permission denied: 'employees.csv'

To fix this error, open the employees.csv file directory in your terminal and run this ls -la command to check permission.

Ls -la

Check also, fix error – type of expression is ambiguous without more context in swift

If you take a look at the response of the above command. You will see that the file is owned by the root user and no other person can have access to it except him/she alone.

rw-rw-rw- 1 root staff 46 Oct 5 07:01 employees.csv

To get it resolve, you need to change the user of the file so that anyone can access it and by doing that we can use our python application to also perform operations on the same file. So type and hit enter on the command-line whiles in the file directory.

chmod 755 employees.csv

This solves the error and if we run application, it will read the content of the file and print it successfully.

That’s how you fix the permission denied errno 13 bug in python

Tagged

About Justice Ankomah

computer science certified technical instructor. Who is interested in sharing (Expert Advice Only)
View all posts by Justice Ankomah →

Leave a Reply

Your email address will not be published. Required fields are marked *