Lingua-e
Python

What is a PermissionError?

← Errors

PermissionError is raised when trying to perform a file or directory operation without the required permissions. It is a subclass of OSError with errno set to EACCES or EPERM. This typically happens when a non-root process tries to access restricted system files.

Why it happens

  • Writing to a file owned by another user or root without write permission.
  • Reading a file with mode 000 or otherwise restricted access.
  • Trying to bind to a privileged port (below 1024) without elevated rights.
  • Deleting a file in a directory where the current user lacks write permission.

Examples

Python

Code that triggers the error

with open("/etc/passwd", "w") as f:
    f.write("hacked")

Error output

PermissionError: [Errno 13] Permission denied: '/etc/passwd'

How to fix it

Check file permissions with ls -l or Path.stat() before operating. Run the process with appropriate privileges if truly needed. Prefer writing to user-owned directories like the home directory or a temp folder.

Fixed code

import os
from pathlib import Path

target = Path("output.txt")
try:
    target.write_text("data")
except PermissionError as e:
    print(f"Cannot write: {e}")

Related errors

Ready to practice your English at work?

Lingua-e has interactive exercises built around real developer conversations: standups, code reviews, retrospectives, and more. Practice until it comes naturally.

Try Lingua-e for free