PermissionError
PythonERRORI/O ErrorHIGH confidence

Operation not permitted

What this means

A subclass of `OSError`, raised when an operation is attempted without the required access rights. This usually relates to filesystem permissions but can also apply to other OS-level resources.

Why it happens
  1. 1Trying to read a file for which you do not have read permissions.
  2. 2Trying to write to a file that is read-only or located in a protected directory.
  3. 3Attempting to bind a network socket to a privileged port (e.g., port 80) without being a root/administrator user.
How to reproduce

This error occurs when attempting to write to a file that the current user does not have write permissions for.

trigger — this will error
trigger — this will error
# To test this, first create a file and make it read-only.
# On Linux/macOS: touch read_only.txt; chmod 444 read_only.txt
# On Windows: Right-click file -> Properties -> Read-only

with open("read_only.txt", "w") as f:
    f.write("This will fail.")

expected output

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
PermissionError: [Errno 13] Permission denied: 'read_only.txt'

Fix 1

Change filesystem permissions

WHEN You have control over the file and need the script to access it.

Change filesystem permissions
# This is a shell command, not Python.
# On Linux/macOS, to make the file writable:
chmod 644 read_only.txt

Why this works

By changing the file's permissions at the OS level, you grant the user running the script the necessary rights to perform the operation.

Fix 2

Run the script as a user with sufficient privileges

WHEN The file or resource is protected and you have access to a privileged account.

Run the script as a user with sufficient privileges
# This is a shell command, not Python.
# Run the script using sudo
sudo python your_script.py

Why this works

Running the script with `sudo` (on Linux/macOS) or as an Administrator (on Windows) elevates its privileges, allowing it to access protected files and resources.

Code examples
Triggerpython
with open("/etc/shadow", "r") as f:
    data = f.read()  # PermissionError: Permission denied
Handle with try/exceptpython
try:
    with open(path, "w") as f:
        f.write(data)
except PermissionError as e:
    print(f"Cannot write: {e}")
Avoid with os.access() checkpython
import os
if os.access(path, os.W_OK):
    with open(path, "w") as f:
        f.write(data)
else:
    print("No write access")
What not to do

Running your entire application as root/administrator just to fix a permission error

This is a major security risk (the Principle of Least Privilege). It's far better to grant only the specific, minimal permissions needed for the task.

Version notes

Sources
Official documentation ↗

cpython/Objects/exceptions.c

Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev

← All Python errors