Home·Error messages

python · Python

The message

PermissionError: [Errno 13] Permission denied

What it means

The operating system refused that operation on that path. You were writing into somewhere owned by someone else such as /var/log or /usr, or a mounted volume in a container whose UID does not match the container user, or — the one people miss — you have no write permission on the directory rather than on the file, which is what creating a new file needs. Check owner and mode with ls -ld first, and before reaching for sudo consider moving the path to somewhere you can write: a file created under sudo raises the same error the next time you open it without sudo.

The fix

ls -ld /var/log/app.log
Printed by
python
Python
18

A Python traceback splits the answer in two — the last line says what went wrong, the frames above it say where — so reading only the last line gives you the name and loses the place, and for the value-is-missing errors such as NoneType and KeyError the cause almost always sits in a frame above the one that crashed.

Reading an error message

  • Read from the first line down. The lower you go the more it is about the tool’s internals; the cause is usually at the top.
  • If there is a file and a line number, start there — not the top stack frame, but the topmost line that names a file you wrote.
  • Search the message verbatim, but strip your own paths and variable names first; those are what stop the search from matching.
  • The same condition is worded differently across tool versions. If results look wrong, add the version number to the query.
  • Before pasting a fix, check what it throws away. Some of these cannot be undone.

Common questions

Q. What does “PermissionError: [Errno 13] Permission denied” mean?

The operating system refused that operation on that path. You were writing into somewhere owned by someone else such as /var/log or /usr, or a mounted volume in a container whose UID does not match the container user, or — the one people miss — you have no write permission on the directory rather than on the file, which is what creating a new file needs. Check owner and mode with ls -ld first, and before reaching for sudo consider moving the path to somewhere you can write: a file created under sudo raises the same error the next time you open it without sudo.

Q. How do I fix it?

ls -ld /var/log/app.log — before running it, check the explanation above for what this command discards.

Q. Which tool prints this?

python. It sits under Python, and the message runs to 5 words.

Errors nearby