How to Solve Errno 13 Error in Python
Table of Contents
When you try to access a file that you don't have permissions to access in Python, you will get an error.
This will happen because Python will try to open the file and will be denied by the operating system.
In this post, we'll learn how to fix this error by granting permission to the file.
Fixing permission errors
First, create a file called, example.txt
and give it the following content:
BASHhello world
This is how we would read that file in Python.
PYTHONf = open("example.txt", "r")
print(f.read())
If you don't have the right permissions, you'll see this:
BASHPermissionError: [Errno 13] Permission denied: 'example.txt'
As mentioned before, we get this error because Python is trying to open the file and is denied access.
Therefore, to solve this, you will have to properly grant yourself access to the file.
If you're on a Unix-based system, you can do this by running the following command:
BASHchown user:user example.txt
You can also use this command:
BASHchmod 775 example.txt
If you ran the command successful, you should have access to the file if you run the Python code as the same user.
PYTHONf = open("example.txt", "r")
print(f.read())
BASHhello world
Conclusion
In this post, we learned how to avoid permission errors when working with files in Python.
The solution is to grant yourself access to the file using the chown
or chmod
command.
Once you have access to the file, you can run the Python code as the same user you granted access to.
Thanks for reading!
- How to Set Up Cron Jobs in Linux
- Best Visual Studio Code Extensions for 2022
- How to deploy a PHP app using Docker
- How to deploy a MySQL Server using Docker
- Learn how to use v-model with a custom Vue component
- Using Puppeteer and Jest for End-to-End Testing
- How to Scrape the Web using Node.js and Puppeteer
- Getting User Location using JavaScript's Geolocation API
- Getting Started with Moment.js
- Learn how to build a Slack Bot using Node.js
- Using Push.js to Display Web Browser Notifications
- Getting Started with React