Python — PermissionError: [Errno 13] Permission denied on file access
Resolve PermissionError by distinguishing file permission issues from directory, ownership and container boundary problems.
Symptoms
- PermissionError: [Errno 13] Permission denied when opening a file
- The failure happens on write but reading works
- The path works in a shell but fails from the application
- The error appears only inside a container or service
Possible causes
- The file or its parent directory lacks write or execute permission for the running user
- The process runs as a different, unprivileged user than your shell
- A mounted volume is read-only or owned by another user
- The path is a directory, not a file, so the open mode is wrong
Diagnosis steps
Identify the effective user and target path
id && ls -ld /path/to/file /path/toInterpretation
The id shows which user runs the process, and ls reveals whether that user can read, write or traverse the path.
Test access with the process user
sudo -u appuser test -w /path/to/file && echo writableInterpretation
Reproducing the access as the service user confirms whether permissions or the process identity is the cause.
Check mount and container options
mount | grep /path ; docker inspect --format "{{.HostConfig.ReadonlyRootfs}}" containerInterpretation
A read-only mount or read-only root filesystem explains writes that fail despite correct ownership.
Root cause
The effective user for the process cannot perform the requested operation on the path, because permissions, ownership, mount options or the user identity differ from your interactive shell.
Fix
Grant the service user the required permissions on the file and its parent directory, correct ownership, or write to a directory intended for runtime data. For read-only mounts, redirect writes to a writable location.
Verify the fix
Rerun the operation and confirm it succeeds, and check that the created or written file has the expected owner and mode.
Symptom walkthrough
Opening a file for writing raises PermissionError: [Errno 13] Permission denied, while the same path works when you test it manually. The discrepancy is the important clue: your shell and the application may run as different users, or the path may be on a read-only mount. Directories need execute permission for traversal, so a missing x bit on a parent directory can block access even when the file itself looks fine.
Fix in practice
Give the service account the rights the operation requires on the file and each parent directory, fix ownership mismatches, and send runtime writes to a directory designated for them. When the mount is read-only, explicitly redirect writes elsewhere instead of fighting the permissions.
Verification
Repeat the write and confirm it succeeds, then inspect the resulting file to confirm it has the expected owner and mode. A writable test as the service user is the most reliable confirmation.
Diagnostic commands
Check the behavior with commands like these:
id && ls -ld /path/to/file /path/to
sudo -u appuser test -w /path/to/file && echo writable
mount | grep /path ; docker inspect --format "{{.HostConfig.ReadonlyRootfs}}" container