Fixing Python's PermissionError: [Errno 13] Permission Denied

beginner🐍 Python2026-04-26| Python 3.x on Windows, Linux, or macOS

Error Message

PermissionError: [Errno 13] Permission denied: '/path/to/file'
#python#file-handling#devops#troubleshooting#windows

Why is this happening?

Ever tried to save a file in Python only to be met with a wall of red text? This error is Python's way of saying it tried to knock on a door, but the Operating System (OS) refused to open it.

Traceback (most recent call last):
  File "script.py", line 5, in <module>
    with open('/path/to/file', 'w') as f:
PermissionError: [Errno 13] Permission denied: '/path/to/file'

Most of the time, your code logic is perfectly fine. The real issue lies in the tug-of-war between your script and your computer's security rules. It usually boils down to one of four common roadblocks.

The Most Likely Culprits

  • Targeting a Folder: You accidentally told Python to "write" to a directory instead of a specific file (e.g., open('/Users/work/data') when 'data' is a folder).
  • Locked Files: Another program, like Excel or a PDF reader, has the file open and won't let Python touch it.
  • Missing Rights: Your user account doesn't have the authority to edit files in system-protected areas like C:\Program Files or /etc.
  • Security Software: Windows Ransomware Protection or third-party antivirus tools are treating your script as a potential threat.

How to Fix It

1. Verify Your Path

Python cannot treat a folder like a text file. If your path points to a directory, open() will fail every time. Use a quick check to see if you need to append a filename.

import os

path = './output_data'

if os.path.isdir(path):
    print(f"Warning: {path} is a directory!")
    # Automatically fix the path
    path = os.path.join(path, 'results.csv')

2. Check for File Locks (Windows)

Windows is notoriously protective of open files. If you try to modify report.xlsx while it is open in Excel, you will trigger Errno 13. Close the application and try again.

Not sure what is holding the lock? Use Resource Monitor:

  • Press Ctrl + Shift + Esc for Task Manager, then go to Performance > Open Resource Monitor.
  • Click the CPU tab.
  • Search for your filename in the Associated Handles box.
  • Right-click the offending process and select End Process.

3. Fix Permissions on Linux and macOS

On Unix-based systems, files often belong to the root user or a different group. Run ls -l in your terminal to see the current ownership. If you see -rw-r--r--, you might not have the 'write' (w) permission you need.

To grant yourself access, change the file ownership to your current user:

# Replace 'youruser' with your actual username
sudo chown youruser:youruser /path/to/your/project/data.json

# Or, give everyone read/write access (use sparingly)
chmod 666 /path/to/your/project/data.json

4. Bypass Ransomware Protection

Windows 10 and 11 feature "Controlled Folder Access" to stop malicious scripts from encrypting your files. Unfortunately, this often blocks legitimate Python scripts too. If you are trying to write to your Documents or Desktop folders and getting denied, this is likely the cause.

  • Open Windows Security and go to Virus & threat protection.
  • Select Manage ransomware protection.
  • Either toggle Controlled folder access to Off, or click Allow an app through... and add your python.exe to the whitelist.

Pro-Active Verification

Before running a heavy data-processing script, use this small snippet to test if the file is actually writable. It’s better to fail early than to crash after an hour of processing.

import os

def check_access(file_path):
    if os.path.exists(file_path):
        return os.access(file_path, os.W_OK)
    return os.access(os.path.dirname(file_path), os.W_OK)

target = 'data/config.json'
if not check_access(target):
    print(f"Access Denied to {target}. Check your OS permissions!")

A Note on Deployment

When moving scripts to a Linux server, octal permissions like 755 or 644 can get confusing quickly. I often use the Unix Permissions Calculator on ToolCraft to visualize what I'm doing. It provides a simple checkbox interface that generates the exact chmod command you need, which helps prevent accidental security holes in production environments.

To keep your scripts professional, always wrap your file operations in a try...except PermissionError block. This allows you to show a clean, helpful message to the user instead of a confusing stack trace.

Related Error Notes