How To Fix Permissionerror Errno 13 Permission Denied Error In Python

PermissionError: [Errno 13] Permission Denied is one of the most common Python errors users encounter when working with files, folders, or system resources. This error occurs when your Python script tries to access, modify, or execute something without the required permissions. It can happen while reading files, writing data, deleting folders, or accessing protected system locations.

For beginners, this error may seem confusing because the script itself may appear correct, but the operating system blocks the action due to security restrictions. The issue often involves incorrect file paths, attempting to open a folder instead of a file, administrator restrictions, or files being locked by another application.

Update Windows Drivers

The good news is that PermissionError is usually straightforward to fix once you identify the cause. By understanding why the error happens and applying the right solution, you can quickly restore your script’s functionality. This guide covers all major solutions for Windows, Linux, and macOS users.

What Causes PermissionError: [Errno 13]?

PC running slow or unstable? Do you want to update drivers?

Several issues can trigger this Python error:

  • Trying to access a directory instead of a file
  • File is currently open in another program
  • Insufficient user account permissions
  • Attempting to write in restricted folders
  • Incorrect file mode usage
  • Antivirus or ransomware protection blocking access
  • Running script without administrator rights
  • File ownership restrictions
  • Read-only file attributes

Understanding the root cause is the first step toward fixing the problem.

Common Examples of PermissionError

This error often appears in situations like:

  • Reading protected files
  • Writing to Program Files or system directories
  • Saving CSV or Excel files currently open elsewhere
  • Deleting restricted folders
  • Accessing root-level directories
  • Editing configuration files without admin rights

Example:

with open("C:/Program Files/data.txt", "w") as file:
file.write("Hello")

This may fail because Program Files is protected by Windows security.

Solution 1: Verify the File Path

Incorrect paths are a major cause of PermissionError.

Steps:

  • Confirm the file exists
  • Ensure you are targeting a file, not a folder
  • Include proper extensions
  • Use raw strings for Windows paths

Correct example:

file_path = r"C:\Users\YourName\Documents\data.txt"

Wrong example:

file_path = "C:\Users\YourName\Documents\data.txt"

Raw strings prevent escape character issues.

Solution 2: Close Files Opened in Other Programs

Repair PC

If the file is open in Excel, Word, Notepad, or another application, Python may be blocked.

Fix:

  • Close the file manually
  • Check Task Manager for background apps
  • Restart your PC if necessary

This is especially common with:

  • Excel spreadsheets
  • CSV files
  • PDFs
  • Images

Solution 3: Run Python as Administrator

PC running slow or unstable? Do you want to update drivers?

Some operations require elevated permissions.

Windows:

  • Right-click Command Prompt
  • Select Run as Administrator
  • Run your script

IDEs:

  • Launch VS Code, PyCharm, or Jupyter as administrator

Linux/macOS:

sudo python script.py

Use admin rights carefully to avoid accidental system changes.

Solution 4: Check File and Folder Permissions

Your user account may lack required access rights.

Windows:

  • Right-click file/folder
  • Properties > Security
  • Edit permissions
  • Grant Full Control if needed

Linux:

chmod 755 filename

macOS:

  • Use Get Info > Sharing & Permissions

Solution 5: Use Correct File Modes

Incorrect file modes can trigger permission conflicts.

Common modes:

  • "r" = Read
  • "w" = Write
  • "a" = Append
  • "rb" = Binary read
  • "wb" = Binary write

Example:

with open("file.txt", "r") as file:
content = file.read()

Trying to write using read mode or vice versa may cause errors.

Solution 6: Move Files to Accessible Locations

System directories often have strict protections.

Better locations:

  • Desktop
  • Documents
  • Downloads
  • User home directory

Avoid:

  • Program Files
  • Windows folder
  • System32
  • Root directories
PC running slow or unstable? Do you want to update drivers?

Using user folders greatly reduces permission problems.

Solution 7: Disable Antivirus or Controlled Folder Access Temporarily

Windows Security and antivirus software may block Python.

Windows Security:

  • Open Windows Security
  • Go to Virus & Threat Protection
  • Manage ransomware protection
  • Disable Controlled Folder Access temporarily

Third-party antivirus:

  • Disable folder protection or web shield temporarily

Re-enable protection after testing.

Solution 8: Change Ownership of the File or Folder

If ownership belongs to another account, access may be denied.

Windows:

  • Right-click file
  • Properties > Security > Advanced
  • Change owner
  • Apply changes

Linux:

sudo chown username filename

This gives your account proper control.

Solution 9: Use with Statement Properly

Improper file handling can leave files locked.

with open("data.txt", "w") as file:
file.write("Example")

Avoid:

file = open("data.txt", "w")
file.write("Example")

The with statement automatically closes files after use.

Solution 10: Check Directory vs File Confusion

Sometimes users accidentally target folders.

Example:

open("C:/Users/Name/Documents", "r")

This causes PermissionError because it is a directory.

Verify:

import os
print(os.path.isfile(path))

Solution 11: Handle Exceptions Gracefully

Adding error handling improves debugging.

Example:

try:
with open("file.txt", "r") as file:
content = file.read()
except PermissionError:
print("Permission denied. Check file access rights.")

This helps identify permission issues clearly.

Platform-Specific Fixes

Windows

  • Run as administrator
  • Disable Controlled Folder Access
  • Check UAC settings
  • Use user folders

Linux

  • Use chmod
  • Use chown
  • Use sudo
  • Check SELinux restrictions

macOS

  • Grant Terminal Full Disk Access
  • Adjust file permissions
  • Check privacy settings

Advanced Debugging Tips

Check access:

import os
print(os.access("file.txt", os.R_OK))

Current working directory:

import os
print(os.getcwd())

Use pathlib:

from pathlib import Path
print(Path("file.txt").exists())

These tools help diagnose access problems faster.

Best Practices to Avoid PermissionError

  • Use accessible user directories
  • Always close files properly
  • Avoid protected system folders
  • Use raw file paths
  • Keep antivirus settings configured
  • Run scripts with correct privileges
  • Test permissions before operations
  • Use proper exception handling

Final Thoughts

PermissionError: [Errno 13] in Python is usually caused by security restrictions, incorrect paths, or improper file handling. Although frustrating, it is rarely a serious coding issue and can often be resolved quickly by adjusting permissions, correcting file paths, or running your script with elevated rights.

By following the solutions above, you can troubleshoot nearly every permission-related Python error across Windows, Linux, and macOS. Good coding practices, proper file management, and understanding operating system security will help you avoid these issues in future projects.

Frequently Asked Questions

Why do I get PermissionError even as administrator?

The file may be locked, owned by another user, or blocked by antivirus software.

Can antivirus software block Python file access?

Yes. Controlled Folder Access and security software frequently restrict Python.

How do I fix PermissionError on folders?

Check ownership, permissions, and ensure you are not trying to open a directory as a file.

What file mode should I use?

Use the correct mode based on your operation: read, write, append, or binary.

Is chmod necessary on Windows?

Not usually, but Windows file security settings serve a similar purpose.

PC running slow or unstable? Do you want to update drivers?

GeeksDigit.Com
Logo