Python can only open, read from, and write to files if an interpreter has the necessary permissions. If you try to open, read from, or write to a file over which Python has no permissions, you’ll encounter the PermissionError: [errno 13] permission denied
error.
In this guide, we discuss what this error means and why it is raised. We’ll walk through an example so you can learn exactly how to solve this error.
PermissionError: [errno 13] permission denied
Computers use file permissions to protect the integrity of files. Some files have restricted access by default. You can change the access permissions of a file at any time.
Let’s say you are working on an important program. You may only want that program to be readable by you. To accomplish this, you could modify the “read” permissions on all the files and folders in your program. This would limit access to your program.
Any file that you try to access from a Python program must be readable by the user or group that is running the file.
If you are running a Python script from a web server process, for instance, you would need to make sure that the user that owns the web server has access to all the files that you reference in your code.
An Example Scenario
We’re going to build a program that reads a list of NFL scores from a file into a program.
We have a file called afc_east.csv which contains the following:
Bills,4,0 Patriots,2,1 Dolphins,1,3 Jets,0,4
Our file is a CSV. We’re going to import the Python csv module into our code so we can work with the file:
import csv
Next, let’s open up our file in our Python program. We can do this using the csv.reader()
statement:
with open("afc_east.csv", "r") as file: reader = csv.reader(file) for r in reader: print(r)
This code will open up the file called afc_east.csv in read mode. Then, the CSV reader will interpret the file. The CSV reader will return a list of values over which we can iterate. We then print each of these values to the console using a for loop and a print()
statement so we can see the contents of our file line-by-line.
Let’s run our program to see if it works:
Traceback (most recent call last): File "test.py", line 3, in <module> with open("afc_east.csv", "r") as file: PermissionError: [Errno 13] Permission denied: 'afc_east.csv'
Our code returns an error.
The Solution
A PermissionError indicates that Python does not have the right permissions to access a file.
Let’s check the permissions of our file. We can do this using the ls -la command:
ls -la
We can see that the afc_east.csv file is owned by the root user:
-rw-rw-rw- 1 root staff 46 Oct 5 07:01 afc_east.csv
This is a problem because we don’t run Python as root. This means that Python cannot read our file. To fix this error, we need to change the ownership permissions of our file using the chown command:
chown james:admin afc_east.csv
This command makes the “james” user and the “admin” group the owners of the file.
Alternatively, we could change the permissions of the file using the chmod command:
chmod 755 afc_east.csv
This command makes our file readable and executable by everyone. The file is only writable by the owner. Let’s try to run our Python script again:
['Bills', '4', '0'] ['Patriots', '2', '1'] ['Dolphins', '1', '3'] ['Jets', '0', '4']
Our code successfully prints out all of the scores from our file.
Conclusion
The PermissionError: [errno 13] permission denied
error occurs when you try to access a file from Python without having the necessary permissions.
To fix this error, use the chmod or chown command to change the permissions of the file so that the right user and/or group can access the file. Now you have the skills you need to solve this error like a pro!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.