How to fix PermissionError: [Errno 13] Permission denied: 'filename' in Python

PythonINTERMEDIATEMEDIUMUpdated Dec 7, 2025ยท 250 views

This error occurs when Python lacks the necessary file system permissions to read, write, or execute a file or directory. The fix depends on whether you need to change file permissions, ownership, or relocate your project.

Quick fix

Fixes PermissionError: [Errno 13] Permission denied: 'filename'

ls -la /path/to/file
ls -la /path/to/directory
  1. Check current file permissions
  2. Change file permissions with chmod
  3. Change file ownership with chown
  4. Use virtual environment with correct permissions
python โ€” error
ls -la /path/to/file
ls -la /path/to/directory
PermissionError: [Errno 13] Permission denied: 'filename'

What this error means

The "Permission denied" error means your Python process tried to access a file or directory but doesn't have the required permissions. This happens when: 1. File/directory permissions are too restrictive (e.g., 644 without write permission) 2. The file is owned by a different user (typically root) than the one running Python 3. Your Python installation or virtual environment is in a protected system directory 4. The file is on a read-only file system (USB drive, CD, etc.) This is a file system access control issue, not a Python bug. The fix is to adjust permissions or ownership using `chmod` and `chown` commands, or to use a virtual environment in your home directory.

Typical symptoms

  • Python scripts fail with permission denied errors
  • Unable to access or modify files or directories
  • Operations work for some users but not others
  • Running with sudo is a temporary workaround

Common causes

  1. File or directory permissions are too restrictive
  2. User doesn't have read, write, or execute permissions
  3. Running script as wrong user (root-owned file accessed by regular user)
  4. Virtual environment in a protected directory
  5. File or folder ownership mismatch after installation

How to fix "PermissionError: [Errno 13] Permission denied: 'filename'"

1Check current file permissions

Check which files or directories are causing the issue:

bash
ls -la /path/to/file
ls -la /path/to/directory

Look for owner and permission patterns (drwxr-xr-x).

2Change file permissions with chmod

Make the file readable and writable by the owner:

bash
chmod u+r /path/to/file      # Add read for owner
chmod u+w /path/to/file      # Add write for owner
chmod u+x /path/to/file      # Add execute for owner

For directories, add execute permission to allow access:

bash
chmod u+x /path/to/directory
3Change file ownership with chown

If files are owned by root but you're running as regular user:

bash
sudo chown -R username:groupname /path/to/directory

Example:

bash
sudo chown -R $USER:$USER ~/.local/lib/python3.9/
4Use virtual environment with correct permissions

Create and use a virtual environment in a user-owned directory:

bash
python3 -m venv ~/myenv
source ~/myenv/bin/activate
pip install package-name

Advanced notes

On shared systems or servers, always create virtual environments in your home directory. Use umask 0022 to ensure new files have proper default permissions. For system-wide installs, use package managers (apt, yum) instead of pip.

Frequently asked questions

What does "PermissionError: [Errno 13] Permission denied: 'filename'" mean?
The "Permission denied" error means your Python process tried to access a file or directory but doesn't have the required permissions. This happens when: 1. , 644 without write permission) 2.
How do I fix "PermissionError: [Errno 13] Permission denied: 'filename'"?
This error occurs when Python lacks the necessary file system permissions to read, write, or execute a file or directory. The fix depends on whether you need to change file permissions, ownership, or relocate your project.
What causes "PermissionError: [Errno 13] Permission denied: 'filename'"?
File or directory permissions are too restrictive User doesn't have read, write, or execute permissions Running script as wrong user (root-owned file accessed by regular user)
Is the "PermissionError: [Errno 13] Permission denied: 'filename'" error serious?
It is a medium-severity error โ€” more of a blocker than a risk. The steps above resolve it.

Official resources & further reading