PermissionError: Permission Denied

PermissionError: [Errno 13] Permission denied: '/etc/config'

Quick Answer

Your process does not have permission to access the file. Check file permissions, run with appropriate privileges, or write to a user directory instead.

Why This Happens

Operating systems enforce file permissions. Trying to write to system directories or read protected files raises PermissionError. Avoid running as root; use appropriate directories.

The Problem

with open('/etc/myapp/config', 'w') as f:
    f.write('setting=value')

The Fix

from pathlib import Path
config_dir = Path.home() / '.myapp'
config_dir.mkdir(exist_ok=True)
with open(config_dir / 'config', 'w') as f:
    f.write('setting=value')

Step-by-Step Fix

  1. 1

    Check file permissions

    Run ls -la on the file.

  2. 2

    Use user directories

    Write to Path.home() or temp directories.

  3. 3

    Fix permissions if needed

    Use chmod, but avoid making files world-writable.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free