PermissionError in Python — What It Means and How to Fix It
What Python's PermissionError exception means and how to resolve it.
Published October 28, 2026
PermissionError is raised when you do not have the OS permission to access a file or directory.
What it means and why it happens
Typical causes: writing to protected folders, files owned by another user, files open in another program (Windows) or read-only mounts.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
PermissionError: [Errno 13] Permission denied: '/etc/hosts'
Practical examples
Code that triggers it
open('/etc/hosts', 'w')The fix
open('output.txt', 'w').close() # write somewhere you ownCheck permissions
ls -l file.txt
chmod u+w file.txt
sudo chown $USER file.txtCommon mistakes
- Running scripts as root to bypass it.
- Trying to open a directory as a file.
- Windows files locked by other processes.
Key takeaways
- PermissionError: PermissionError is raised when you do not have the OS permission to access a file or directory.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Is sudo the answer?
Rarely; fix ownership or write to a user-owned location.
How do I find which line caused PermissionError?
The error message and stack trace show the file and line; start there and work backwards.