PythonBeginnerMedium severity
PermissionError: [Errno 13] Permission denied
Python was blocked from reading, writing, or executing a filesystem resource.
5-15 min Popularity 79/100 Verified 2026-07-21
What does this error mean?
The operating system rejected Python's requested file operation because the process lacks permission, the path is a directory, or another application has locked the file.
Why does this happen?
- The process lacks read or write permission.
- The code passed a directory path where a file path was expected.
- Another process has locked the file on Windows.
- The destination is protected by the operating system.
- A container volume has incompatible ownership.
AI explanation
Plain-English analysis
Python asked the operating system to open a path. The operating system refused before Python could read or write anything. Inspect the exact path and operation mode first.
Quick fix
Terminal
python -c "from pathlib import Path; p=Path('output.txt'); print(p.resolve(), p.is_dir())"Expected output: The command confirms the exact resolved path and whether it is a directory.
Step-by-step fix
1Use a writable file pathWrite inside the application data directory or a user-owned location.69%
2Close the program locking the fileSpreadsheet applications and editors can hold exclusive locks on Windows.48%
3Correct directory ownershipSet appropriate ownership for mounted volumes or service directories.31%
Alternative solutions
Windows
Close applications using the file Choose a path under the user profile
Do not solve routine application writes by always running as Administrator.
Linux
ls -ld <path> chown <service-user> <path>
Prefer correct ownership over broad chmod 777 permissions.
Docker
RUN chown -R app:app /app/data
Match volume permissions to the non-root runtime user.
Real example
Broken
python
with open("C:/Windows/System32/output.txt", "w") as file:
file.write("done")Corrected
python
from pathlib import Path
output = Path.home() / "devfixes" / "output.txt"
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("done", encoding="utf-8")Frequently asked questions
Only when the task genuinely requires elevated access. For normal application files, use a user-owned path and correct permissions.