Easy Solutions to fix error “PermissionError: [Errno 13] Permission denied”

Python is a wonderful programming language that has been used to design a variety of projects. Programmers all over the world are using Python for programming. It is a language that has easy syntax, but if you are working on it, you may encounter different types of errors. The error “PermissionError: [Errno 13] Permission denied” is one of those warning that pops up after compiling the code.

Python allows you to read, write, and open files only if the Python interpreter has permission to do so. Let’s have a look at how the error occurs

How the error shows up

You can get a better idea of how this error usually occurs by going through the program code

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python34\lib\tkinter\__init__.py", line 1538, in __call__
return self.func(*args)
File "C:/Users/Marc/Documents/Programmation/Python/Llamachat/Llamachat/Llamachat.py", line 32, in download
with open(place_to_save, 'wb') as file:
PermissionError: [Errno 13] Permission denied: '/goodbye.txt'

You get this when you are running the following program

def download():
    # get selected line index
    index = films_list.curselection()[0]
    # get the line's text
    selected_text = films_list.get(index)
    directory = filedialog.askdirectory(parent=root, 
                                        title="Choose where to save your movie")
    place_to_save = directory + '/' + selected_text
    print(directory, selected_text, place_to_save)
    with open(place_to_save, 'wb') as file:
        connect.retrbinary('RETR ' + selected_text, file.write)
    tk.messagebox.showwarning('File downloaded', 
                              'Your movie has been successfully downloaded!' 
                              '\nAnd saved where you asked us to save it!!')

This is exactly how you get the error. Now let’s check out how to fix it

How to fix error “PermissionError: [Errno 13] Permission denied”

The cause of the error is the permission from Python to open, read, and write a file. Without the required permission, you will end up with an error warning. Another reason that pops up the error is when you by mistake pass a folder rather than a file. Another possibility to get the error is that your file may have been opened already by another process.

Now have a look at the solution to fix it

Solution 1 – No permission on the file

When you don’t have permission to read the file, your code to get the issue fixed.

Example Scenario

# 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'

The execution of the script in admin/root (elevated mode) causes the error.

Windows users can solve the error warning by opening the command prompt in the admin mode, and after executing the script, your issue can be solved.

Linux users should follow the command below

ls -la

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

In this example, Python can’t read the file as the root users have permission. It is not recommended to use Python as a root user.

To fix the issue, follow the below command

chmod 755 python.txt

To give permission to specific users, use the below command

chown srinivas:admin python.txt

Once done, you get the issue solved. Check out the output

Dear User,

Welcome to Python Tutorial

Have a great learning !!!

Cheers

Solution 2 – Check the file path

You need to follow the code if you want to check the file path

Use

import os 
path = r"C:\Users\ssc\Desktop\my_personal_file\bio.txt" 
assert os.path.isfile(path) 
with open(path, "r") as f: // Error 
    pass

Instead of

import os 

path = r"C:\Users\ssc\Desktop\my_personal_file" 
assert os.path.isfile(path) 
with open(path, "r") as f: // Error 
    pass

Solution 3 – Check file status

Another solution to fix the issue is to check the status of the file if it is open or closed. You need to make sure it is closed from everywhere to get the error fixed.

Solution 4 – Grant permission

To grant permission, you need to create a shortcut of the python.exe extension. Once done, select properties by right click. Now adjust the path

C:\path_to\your_script.py

or

C:\path_to\python.exe

After that, click the ‘advanced’ option, and then run as administrator. This way you can make the error go away.

Conclusion

And that is how you can solve the error “PermissionError: [Errno 13] Permission denied”. I hope you find it helpful!

Reference Source: https://thewebdev.info/2022/04/16/how-to-fix-permissionerror-errno-13-permission-denied-with-python-open/

https://www.edureka.co/community/177335/permissionerror-errno-13-permission-denied

Leave a Reply

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