PermissionError in Python: Causes and Fixes

PermissionError means Python tried to access a file or folder, but your user account was not allowed to do it.

This usually happens when you:

  • open a protected file
  • write to a restricted folder
  • try to rename or delete something you do not control
  • pass a folder path to open() by mistake
  • use a file that another program is already using

A common message looks like this:

PermissionError: [Errno 13] Permission denied

The goal is to quickly find out whether the problem is:

  • the path
  • the file type
  • file permissions
  • or another program locking the file

Quick fix

Try this first:

from pathlib import Path

path = Path("example.txt")

if path.exists() and path.is_file():
    with open(path, "r", encoding="utf-8") as file:
        print(file.read())
else:
    print("Check the path and permissions.")

Use a real file path, make sure it is a file and not a folder, and confirm your user account has permission to read it.

What PermissionError means

PermissionError happens when Python tries to access something the current user is not allowed to use.

Important points:

  • It often appears while opening, writing, renaming, or deleting files and folders.
  • It is a subclass of OSError.
  • The most common message is [Errno 13] Permission denied.

In simple terms, Python is saying: "I found this path, but I am not allowed to use it in the way you asked."

When this error usually happens

You will often see PermissionError in situations like these:

  • Opening a file without read permission
  • Writing to a file or folder without write permission
  • Trying to delete or rename a protected file
  • Trying to open a folder as if it were a file
  • Accessing system locations that require admin rights
  • Trying to use a file that another program has locked

If you are new to file handling, it also helps to understand the basics of Python file handling: read and write and the Python open() function.

Example that causes PermissionError

Example 1: Opening a protected file

with open("/root/secret.txt", "r", encoding="utf-8") as file:
    print(file.read())

On many systems, this may fail with:

PermissionError: [Errno 13] Permission denied: '/root/secret.txt'

Example 2: Writing inside a restricted folder

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

This can raise:

PermissionError: [Errno 13] Permission denied: '/system_file.txt'

Example 3: Opening a directory by mistake

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

On some systems, this raises:

PermissionError: [Errno 13] Permission denied: '.'

The traceback will point to the exact line that failed. Read that line carefully. It tells you which file operation caused the problem.

Fix 1: Check the file path carefully

A very common cause is using the wrong path.

Make sure:

  • the path exists
  • the path points to a file when using open()
  • you did not accidentally pass a folder path

Use pathlib to check:

from pathlib import Path

path = Path("example.txt")

print(path)
print(path.exists())
print(path.is_file())
print(path.is_dir())

if path.exists() and path.is_file():
    with open(path, "r", encoding="utf-8") as file:
        print(file.read())
else:
    print("The path is missing or it is not a file.")

What this does:

  • exists() checks whether the path is real
  • is_file() checks whether it is a file
  • is_dir() checks whether it is a folder

If you want to get better at this, see working with file paths in Python.

Fix 2: Use a location your user can access

If your code writes to a protected folder, move the file to a normal location first.

Good beginner-friendly places:

  • your project folder
  • your home folder
  • your desktop
  • a folder you created yourself

Example:

with open("notes.txt", "w", encoding="utf-8") as file:
    file.write("This file is in the current project folder.")

This is much safer than trying to write into system folders.

If you are trying to save data, see how to write to a file in Python.

Fix 3: Close programs that may be using the file

Sometimes the path and permissions are correct, but another program is using the file.

This is common when the file is open in:

  • a text editor
  • Excel or another spreadsheet program
  • another Python script
  • a backup or sync tool

This often affects:

  • writing to a file
  • renaming a file
  • deleting a file

If you think a file is locked:

  1. Close any program that may be using it.
  2. Run your script again.
  3. If needed, restart the program or your computer.

This issue is especially common on Windows.

Fix 4: Check file and folder permissions

Your account needs the right permission for the action you want:

  • read permission to open a file for reading
  • write permission to change or save a file
  • permission on the folder when creating or deleting files

On Linux and macOS

You can inspect permissions in the terminal:

ls -l example.txt

This shows who can read, write, or execute the file.

On Windows

You can:

  1. Right-click the file or folder
  2. Open Properties
  3. Go to Security
  4. Check whether your user account has the needed access

Only change permissions if you understand what you are changing. A safer first step is to move the file into a folder you already control.

Fix 5: Avoid running code in restricted system locations

Some folders are protected by the operating system.

Examples include:

  • system directories
  • application install folders
  • other users' folders
  • admin-only locations

For beginner code, do not start there.

Instead:

  • create a normal project folder
  • store your test files there
  • make sure you can manually open and edit those files

Running Python as administrator is usually not the best first fix. It can hide the real problem and make your code harder to test safely.

How to debug PermissionError step by step

When you see this error, go through these checks in order.

1. Print the exact path

print(path)

Make sure Python is using the path you expect.

2. Check whether the path exists

from pathlib import Path

p = Path(path)
print(p.exists())

3. Check whether it is a file or directory

print(p.is_file())
print(p.is_dir())

If is_dir() is True, do not pass that path to open().

4. Check your current working folder

Sometimes a relative path points somewhere unexpected.

import os
print(os.getcwd())

5. Try read-only access first

If writing fails, test with reading first:

from pathlib import Path

path = Path("example.txt")

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

6. Try the same code with a simple local file

Put a test file in your project folder and try again.

If that works, the problem is likely the original path or permissions.

7. Read the full traceback

The traceback tells you:

  • which line failed
  • which path caused the issue
  • what operation Python was trying to do

Example of handling the error with try-except

You can catch PermissionError and show a clearer message.

from pathlib import Path

path = Path("example.txt")

try:
    with open(path, "r", encoding="utf-8") as file:
        print(file.read())
except PermissionError:
    print("Permission denied.")
    print("Check the file path, file permissions, and whether another program is using the file.")
except FileNotFoundError:
    print("The file was not found.")

This helps users understand what to check next.

If you need help with missing files too, see FileNotFoundError in Python: causes and fixes.

Some file errors look similar but mean different things.

FAQ

What is PermissionError in Python?

It means Python was blocked from accessing a file or folder because of permission rules or file locking.

Why do I get [Errno 13] Permission denied?

Usually because:

  • the file is protected
  • the folder is restricted
  • the path points to a directory
  • another program is using the file

Can opening a folder with open() cause PermissionError?

Yes. If you pass a directory path to open(), some systems raise PermissionError instead of reading a file.

Should I run Python as administrator to fix this?

Not as a first step. First check:

  • the path
  • whether it is a file or folder
  • permissions
  • whether another program has locked the file

How do I avoid PermissionError as a beginner?

The safest approach is to:

  • use files inside your project folder
  • verify the path before opening it
  • close apps that may already be using the file

See also