ProjectStack
python

PermissionError: [Errno 13] Permission denied

Python tried to read, write, or execute a file or directory but the operating system blocked it due to insufficient permissions. The file is owned by a different user or is protected.

Common causes

  • Trying to write to a system directory (like /etc or C:\Windows) without administrator rights
  • The file is owned by root or another user and your account can't write to it
  • Attempting to overwrite a read-only file
  • On Windows, the file is locked by another process or protected by UAC

How to fix it

  1. Check file permissions: ls -la <file> (Mac/Linux) or right-click → Properties (Windows)
  2. Change ownership or permissions: sudo chown $(whoami) <file> or chmod 644 <file>
  3. Write to a directory you own, like your home directory or /tmp
  4. On Windows, run your terminal as Administrator if you genuinely need access to that location

Example

Traceback (most recent call last): File "app.py", line 3, in <module> with open('/etc/hosts', 'w') as f: PermissionError: [Errno 13] Permission denied: '/etc/hosts'

Trying to write to /etc/hosts without root privileges

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.