Python PermissionError: [Errno 13] Permission denied Fix

The PermissionError: [Errno 13] permission denied to open, read, or write files will be encountered when we provide a folder path instead of a file path when reading a file or when Python does not have the required permissions to perform file operations (open, read, or write).

PermissionError: [Errno 13] Permission denied error is discussed in this article, along with examples of how to resolve this error.

How To Fix PermissionError: [Errno 13] Permission denied Error?

Let us write an example code that would throw the above error message and then we will see how we can fix that error quickly.

1. Insufficient Privilege Issues

Consider a scenario in which you have a local CSV file containing confidential or sensitive data that must be kept secure. If you want to make a file only accessible to you, you can change the file permissions. In order to read the file, we’ll write a Python program that will print its contents.

# Program to Read A file
# Make Sure that Python is not running
# In Admin or Root Mode.
demoFile = open("demoFile.txt", "r")

getContentOfFile = demoFile.read()

print(getContentOfFile)

demoFile.close()

Output:

Traceback (most recent call last):
  File "C:/Coduber/demoFile.txt", line 4, in <module>
    demoFile = open("demoFile.txt", "r")
PermissionError: [Errno 13] Permission denied: 'demoFile.txt'

When we run the code, we receive the following error: PermissionError: [Error code 13] Because the file is created by the root user, a permission denied error occurs. In this case, we are not running the script in elevated mode (admin/root).

If you are using Windows, you can resolve this issue by opening the command prompt in administrator mode and running the Python script to resolve the issue.

In the case of Linux, we can work around the problem by using the sudo command to run the script as the root user. Running the following command will also allow you to determine whether a file’s permissions have been granted.

ls -la

In the preceding example, the file is owned by the root user, and we are not running Python as the root user, so Python is unable to read the file.

We can resolve the problem by modifying the permissions for a specific user or for all users at the same time. The following command will make the file readable and executable to anyone who has access to it.

chmod 755 demoFile.txt

In addition, we can restrict access to specific users rather than making it available to the general public. This can be accomplished by executing the following command.

chown yourUserName:admin demoFile.txt

The following output will be produced when our code is rerun after the appropriate permissions have been granted:

This is Test File.

2. Providing Folder Path Instead Of File Path

As shown in the following example, we provided a folder path rather than a valid file path, resulting in the Python interpreter returning an Errno 13 permission denied error.

# Program to Read A file
# Make Sure that Python is not running
# In Admin or Root Mode.
demoFile = open("C:\\Users\\Coduber\\", "r")

getContentOfFile = demoFile.read()

print(getContentOfFile)

demoFile.close()

Output:

Traceback (most recent call last):
  File "c:\Users\Coduber\test.py", line 4, in <module>
    demoFile = open("C:\\Users\\Coduber", "r")
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\Coduber'

It is possible to resolve the error by providing a valid file path; however, if we accept the file path dynamically, we must change our code in order to verify that the given file path is a valid file before processing it.

# Program to Read A file
# Make Sure that Python is not running
# In Admin or Root Mode.
demoFile = open("C:\\Users\\Coduber\\demoFile.txt", "r")

getContentOfFile = demoFile.read()

print(getContentOfFile)

demoFile.close()

Output:

This is Test File.
Python PermissionError: [Errno 13] Permission denied Fix

3. File Is Already Open By Other Program

If we forget to close the file after performing file operations in Python, it will remain open indefinitely. As a result, we will receive a permission denied error the next time we attempt to access the file because it is already in use by another process and we did not close the file.

Ensure that a file is closed after an i/o operation is performed on it in order to avoid this error from occurring again. For more information on how to read files in Python and how to write files in Python, please see the following articles.

Wrap Up

You will receive the PermissionError: [Errno 13] Permission denied error in Python if you attempt to read a file from a folder path rather than a file path or if the Python does not have the required permissions to perform file operations (open, read, and write).

It is possible to resolve this error by ensuring that the file has been given appropriate permissions by using the chown or chmod commands, as well as by ensuring that Python is running in elevated mode.

Please let me know in the comments if you are still experiencing this problem or if you have a better solution than the one discussed above. I’ll gladly include it here.

Further Read:

  1. Python ValueError: could not convert string to float [Fix]
  2. Python pip: command not found Quick Fix
  3. Python Write Text File
  4. [Fixed] Python TypeError: ‘NoneType’ Object Is Not Subscriptable
  5. [Fixed] Python TypeError: ‘list’ object is not callable

Leave a Comment