PermissionError
This error occurs when you attempt to access a file or system resource without the necessary read, write, or administrative permissions.
Usually happens because:
- ☑ Accessing or writing to a file owned by root/another user
- ☑ Passing a directory folder path directly to open() function
- ☑ Binding sockets to privileged port values below 1024
🔍 Quick Checklist:
What is PermissionError?
The 'PermissionError' is raised when a file system or system call fails due to lack of authorization. This is the Python equivalent of POSIX errno EACCES or EPERM. It commonly happens when trying to write to files owned by 'root' without administrative elevation, attempting to read a restricted system file, or trying to bind a server socket to a privileged system port (like port 80 or 443) as a standard user.
Common Causes
- Insufficient file access permissions: Attempting to write to a read-only file or one owned by another system user (e.g. root).
- Accessing restricted folders or system files: Attempting to write to root directories like '/etc/' or '/usr/bin/' directly.
- Privileged port binding: Attempting to bind a network server to a port below 1024 without root privileges.
| Cause | Frequency |
|---|---|
| Writing to system root directories without sudo | ⭐⭐⭐⭐⭐ |
| Privileged network port binding (port < 1024) | ⭐⭐⭐⭐ |
| Target file lacks user write permissions (chmod) | ⭐⭐⭐ |
Common Mistakes
- Running `open()` on directory paths instead of filenames, which returns a generic PermissionError on Unix environments.
- Using raw redirects (e.g. `sudo python script.py > /etc/out.txt`) where the output file redirection runs as standard user, triggering access denies.
How to Fix
Python Operations & Verification
Ensure your paths reference files instead of directories to avoid triggering system permissions blocks.
# Wrong: passing a folder path to open() throws PermissionError
# with open("/var/log", "r") as f:
# data = f.read()
# Correct: reference a specific file in the folder
with open("/var/log/syslog", "r") as f:
data = f.read()Platform Specific Fixes
Grant network binding capabilities to the python executable to allow port 80/443 mapping without root.
sudo setcap 'cap_net_bind_service=+ep' $(which python3)Best Practices
- Map system variables to user configuration folders (like `~/.config/`) instead of global directories.
- Adopt ports above 1023 for developer configuration environments.
Frequently Asked Questions (FAQ)
Q: What is a PermissionError?
It is a runtime exception raised when the operating system refuses a request from Python to access a file, directory, or network resource due to security restrictions.
Q: How do I fix 'PermissionError: [Errno 13] Permission denied'?
This usually means the file or directory is owned by 'root' or another user. You can run your script with administrative privileges ('sudo python script.py') or change the file ownership to your current user: 'sudo chown $USER filename'.
Q: Why does binding port 80 throw this error?
On Linux/Unix systems, ports below 1024 are 'privileged'. Standard users cannot bind to them. Either run as root, bind to a higher port (like 8080), or grant the Python binary binding capabilities using 'setcap'.
Q: Why do I get PermissionError when opening a directory?
This happens if you pass a directory path to the 'open()' function instead of a file path. Make sure your path references a file.