How to Fix: PermissionError: [Errno 13] Permission denied

3D visualization of a secure vault door denying access to a user, representing the Python PermissionError.

This error means Python tried to access a file or folder, resulting in a PermissionError: Permission denied, but the Operating System said “NO!”

โšก Quick Fix: PermissionError: [Errno 13] Permission denied โ€” Python File Locked by Excel, Wrong Path Type, and Admin Rights Fix

The OS blocked Python’s file access โ€” the file is open in another program, your path points at a folder instead of a file, or the target folder requires admin rights.

# WRONG โ€” data.csv is open in Excel, Python can't write to a locked file
with open("data.csv", "w") as f:
    f.write("new data")    # PermissionError: [Errno 13] Permission denied

# WRONG โ€” path points at a folder, not a file
with open("C:/Users/Name/Documents/my_folder", "w") as f:
    f.write("Hello")       # PermissionError โ€” my_folder is a directory

# WRONG โ€” writing to a system-protected folder
with open("C:/Program Files/my_app/config.txt", "w") as f:
    f.write("config")      # PermissionError โ€” OS blocks this without admin

# RIGHT โ€” close Excel first, target a file path, write to a folder you own
with open("C:/Users/Name/Documents/my_folder/data.txt", "w") as f:
    f.write("new data")    # works

The three causes below show you exactly how to diagnose which one hit you and the safest fix for each.

Cause 1: The File is Open Elsewhere (Most Common on Windows)

If you have data.csv open in Excel, and you try to run a Python script that writes to that same data.csv, it will fail. Excel “locks” the file so nothing else can change it while you’re working.

The Fix: Close the file in all other programs (Excel, Word, etc.) and run your script again.

Cause 2: Trying to Write to a Folder, Not a File

Sometimes you mix up your paths.

# 'my_folder' is a directory, not a file!
with open("C:/Users/Name/Documents/my_folder", "w") as f:
    f.write("Hello")
# CRASH! PermissionError

The Fix: Make sure your path includes the actual filename at the end: .../my_folder/data.txt.

Cause 3: Actual Permissions (Admin Rights)

You might be trying to write to a protected system folder (like C:\Program Files on Windows or /etc/ on Linux) without administrator privileges.

The Fix: Either run your terminal/IDE as Administrator (not recommended for security reasons) or, better yet, save your files to a folder you own, like Documents or Desktop.


PermissionError: [Errno 13] Permission denied โ€” Three Checks, 60 Seconds, Fixed

PermissionError: [Errno 13] Permission denied always comes from the OS, not from Python. Python asked for file access. The OS said no. Your job is to find out why.

Run these three checks in order.

Check if the file is open in another program. On Windows, Excel, Word, and any PDF reader locks files while they’re open. Close the file completely โ€” not just minimized โ€” then rerun your script. This fix resolves the error for the majority of developers hitting this page.

Check your path ends with a filename. Open your path string and read the last segment. A folder name with no extension means Python is trying to open a directory as a file. Add the filename: โ€ฆ/my_folder/output.txt, not โ€ฆ/my_folder.

Check where you’re writing. C:/Program Files, C:/Windows, and /etc/ on Linux require admin rights your script doesn’t have. Move your output to a folder you own โ€” Documents, Desktop, or a dedicated project folder inside your home directory. Writing to system folders from a Python script is the wrong architecture regardless of whether the permission error fires.

For scripts that handle files regularly, wrap every open() call in try/except PermissionError to catch this at runtime and give users a clean error message instead of a raw traceback.

try:
with open(file_path, “w”) as f:
f.write(data)
except PermissionError:
print(f”Access denied: close {file_path} in other programs and try again.”)

Similar Posts

Leave a Reply