![]()
A PermissionError occurs when Python tries to access, modify, or delete a file or directory but lacks the required permissions. This error can happen due to:
- Trying to open a file without read/write permission
- Attempting to modify a protected system file
- Opening a file in write mode without necessary access
- Trying to execute a script without execution permission
- Using a directory as a file (e.g.,
open("folder"))
1. Common Causes and Fixes
Cause 1: Insufficient Permissions to Read/Write the File
If you don’t have permission to read or write to a file, Python will throw a PermissionError.
Incorrect Code:
with open("/root/protected.txt", "r") as file:
content = file.read()
Problem: The file is in a restricted directory.
Solution: Run the script as an administrator/root or change permissions.
Fix: Change File Permissions
On Linux/macOS, use:
sudo chmod +r protected.txt # Grant read access
sudo chmod +w protected.txt # Grant write access
On Windows, right-click the file → Properties → Security → Edit Permissions.
Cause 2: Trying to Write to a Read-Only File
If a file is read-only, writing to it will raise a PermissionError.
Fix: Modify File Permissions
chmod +w readonly.txt # Linux/macOS: Add write permission
Or, change file attributes on Windows:
attrib -R readonly.txt
Cause 3: Attempting to Open a Directory as a File
If you mistakenly try to open a directory instead of a file, Python will throw an error.
Incorrect Code:
with open("my_folder", "r") as file:
content = file.read()
Problem: "my_folder" is a directory, not a file.
Solution: Ensure the path points to a valid file.
import os
path = "my_folder"
if os.path.isdir(path):
print("This is a directory, not a file!")
else:
with open(path, "r") as file:
content = file.read()
Cause 4: Trying to Write to a Restricted System File
Some files, like system logs or program files, require admin privileges to modify.
Fix: Run as Administrator
- On Linux/macOS, use
sudo: bashCopyEditsudo python3 script.py - On Windows, run Python as Administrator:
Right-click Python → Run as Administrator.
Cause 5: Trying to Delete a Read-Only File
If a file is marked as read-only, Python cannot delete it.
Fix: Remove Read-Only Attribute
- Linux/macOS:
chmod +w myfile.txt # Add write permission rm myfile.txt # Now delete - Windows:
attrib -R myfile.txt del myfile.txt
Cause 6: Locked File in Use by Another Program
If another process is using the file, Python cannot access it.
Fix: Close the file before running Python
- Close it manually if open in an editor.
- Check which program is using the file:
- Windows: Use
Resource Monitor - Linux/macOS: Use
lsofbashCopyEditlsof myfile.txt
- Windows: Use
- Restart your system to unlock the file.
2. Handling PermissionError in Python
Use Try-Except to Handle the Error Gracefully
try:
with open("protected.txt", "w") as file:
file.write("Hello!")
except PermissionError:
print("Permission denied! Try running as admin or changing file permissions.")
Use os.access() to Check Permissions Before Accessing
import os
file_path = "data.txt"
if os.access(file_path, os.W_OK):
with open(file_path, "w") as file:
file.write("Hello, world!")
else:
print("You do not have write permission for this file.")
3. Summary of Fixes
| Issue | Fix |
|---|---|
| No read/write permission | Change file permissions (chmod / attrib -R) |
| Opening a directory as a file | Check with os.path.isdir() |
| Writing to a read-only file | Grant write access (chmod +w) |
| Deleting a protected file | Remove read-only flag (attrib -R) |
| File locked by another program | Close the file or restart system |
| Running script without admin rights | Run as administrator (sudo python3 script.py) |
