PermissionError: Errno 13 Permission denied (Fix)

PermissionError: [Errno 13] Permission denied means Python asked the operating system to access a file, folder, or other resource, but the operating system refused.

This usually happens when:

  • your program does not have permission to read or write something
  • the path points to a folder instead of a file
  • you are trying to save in a protected location
  • another program is locking the file

The good news is that this error is often easy to fix once you check the path, file type, and location.

Quick fix

Start with a simple file path that you know you can access:

file_path = "example.txt"

with open(file_path, "r", encoding="utf-8") as file:
    content = file.read()
    print(content)

First check that:

  • the path is correct
  • the path points to a file, not a folder
  • your user account has permission to read or write that location

If needed, review how file paths work in Python and the open() function explained.

What this error means

This error means Python tried to do something like:

  • open a file
  • create a file
  • modify a file
  • delete a file
  • rename a file
  • access a folder

But the operating system blocked the action.

In beginner code, this is usually related to file or folder permissions. It can also happen if you try to write to a protected system location, such as a system folder or a file owned by another user.

Common situations that cause it

Here are the most common reasons:

  • Trying to write to a file or folder you do not own
  • Trying to save a file in a protected system directory
  • Using a folder path where Python expects a file path
  • Trying to open a file that is locked by another program
  • Trying to access a file with the wrong mode, such as writing where only reading is allowed

Common causes at a glance:

  • Wrong file path points to a folder instead of a file
  • No permission to read the file
  • No permission to write to the target folder
  • File is open or locked by another program
  • Trying to modify a protected system file
  • Using open() on a directory path

Example that causes the error

Opening a protected file with write mode can fail:

path = "/protected-folder/example.txt"

with open(path, "w", encoding="utf-8") as file:
    file.write("Hello")

If your account cannot write to that folder, Python may raise:

PermissionError: [Errno 13] Permission denied

Another common mistake is passing a directory to open() instead of a file:

path = "my_folder"

with open(path, "r", encoding="utf-8") as file:
    print(file.read())

On some systems, this can raise a permission-related error. On others, you may get a different error such as IsADirectoryError: Errno 21 Is a directory.

Deleting or renaming files in restricted locations can also cause the same problem.

How to fix it

Work through these checks one by one:

  • Check that the path points to a real file, not a directory
  • Make sure the file or folder exists in a location you can access
  • Use a different folder such as your home directory, Desktop, or project folder
  • Close other programs that may be using the file
  • Check whether you need read mode or write mode
  • Run the script with the correct user account
  • Only use admin or sudo access when truly necessary

A safer beginner approach is to work inside your project folder:

path = "notes.txt"

with open(path, "w", encoding="utf-8") as file:
    file.write("This file is in my project folder.")

This usually works because you normally have permission to create and edit files in your own working folder.

If the file might not exist, also check how to test whether a file exists in Python. If the path is wrong, you may see FileNotFoundError: Errno 2 No such file or directory instead.

How to debug step by step

When you are not sure what is wrong, print and test the path directly.

1. Print the exact path

path = "example.txt"
print(path)

This helps you confirm the program is using the path you expect.

2. Check whether the path exists

import os

path = "example.txt"
print(os.path.exists(path))

If this prints False, the path is wrong or the file does not exist.

3. Check whether it is a file or a directory

import os

path = "example.txt"

print("Exists:", os.path.exists(path))
print("Is file:", os.path.isfile(path))
print("Is directory:", os.path.isdir(path))

This is important because open() expects a file path, not a directory path.

4. Check your current working folder

import os

print(os.getcwd())

Sometimes the relative path is fine, but your script is running from a different folder than you expected.

5. Try a simple read test

path = "example.txt"

try:
    with open(path, "r", encoding="utf-8") as f:
        print(f.read())
except Exception as e:
    print(type(e).__name__)
    print(e)

This shows the full error clearly and helps you confirm whether the problem is the path, the file type, or permissions.

Safe beginner advice

If you are new to Python, these habits will help you avoid permission problems:

  • Prefer working inside your own project folder
  • Avoid protected locations until you understand permissions better
  • Do not solve every permission problem by running as administrator
  • If the path comes from user input, validate it before using it

Running as administrator or with sudo can sometimes make the error go away, but it is not the best first fix. It is safer to use files and folders that your account already controls.

If you are dealing with different path problems, you may also want to read NotADirectoryError: Errno 20 Not a directory or the broader guide to OSError in Python: causes and fixes.

FAQ

Why do I get PermissionError even when the file exists?

Because existence and permission are different. The file may be there, but your program may not be allowed to read or change it.

Can a folder path cause this error?

Yes. If you pass a directory to open(), Python may fail because open() expects a file path.

Should I always run Python as administrator to fix this?

No. It is better to use a file location you already have access to. Admin access should be a last resort.

Can another program cause PermissionError?

Yes. A file can be locked by another app, which can stop Python from reading, writing, renaming, or deleting it.

See also