How to Fix: PermissionError: [Errno 13] Permission Denied in Python
Last Updated :
02 Aug, 2024
When your Python code encounters a situation where it lacks the necessary permissions to access a file or directory, it raises PermissionError: [Errno 13] Permission denied in Python. This article will explore how to address the Errno 13 Error in Python effectively.
What is PermissionError: [Errno 13] Permission Denied in Python?
PermissionError: [Errno 13] Permission Denied denotes a situation where a program attempts to execute an operation—such as file reading or writing—without the requisite permissions. This error typically arises when a user lacks the necessary privileges to access, modify, or execute a particular file or directory. The error message associated with Errno 13 explicitly indicates that permission is denied for the specified operation.
Error Syntax
PermissionError: [Errno 13] Permission denied
below, are the reasons for the occurrence of PermissionError: [Errno 13] Permission denied in Python:
- Improper File Path Handling
- Incorrect File Content in Python
Improper File Path Handling
This Python code opens a file named "GFG.txt" for writing and writes "Hello, world!" into it. However, the file path contains backslashes (\), which might cause issues if not handled correctly.
Python
with open(r"C:\Users\R.Daswanta kumar\OneDrive\Pictures\GFG\file\GFG.txt", "w") as file:
file.write("Hello, world!")
Output:

Incorrect File Content with Python
Below, code to overwrite the content of a file named "GFG.txt" located at a specific path. If executed without sufficient permissions, Python will raise a PermissionError (Error 13) indicating a permission denied error.
Python
file_path = r"C:\Users\R.Daswanta kumar\OneDrive\Pictures\GFG\file\GFG.txt"
with open(file_path, "w") as file:
new_content = "New content to replace the existing content."
file.write(new_content)
print("File content modified successfully!")
Output:

Solution for PermissionError: [Errno 13] Permission Denied in Python
Below, are the approaches to solve PermissionError: [Errno 13] Permission Denied in Python:
Proper File Path Handling
Below, code defines a file path and opens a file named "GFG.txt" in write mode, enabling it to overwrite existing content. It then replaces the content with the specified new text and confirms successful modification.
Python
file_path = r"C:\Users\R.Daswanta kumar\OneDrive\Pictures\GFG\file\GFG.txt"
with open(file_path, "w") as file:
new_content = "New content to replace the existing content."
file.write(new_content)
print("File content modified successfully!")
Output
File content modified successfully
Correct File Content in Python
The following code first checks if the file exists, then attempts to modify its permissions to allow writing. If successful, it proceeds to overwrite the file's content with new text. If the user lacks necessary permissions, it prints a message indicating permission denial. Finally, it confirms both the modification of file permissions and the content.
Python
import os
file_path = r"C:\Users\R.Daswanta kumar\OneDrive\Pictures\GFG\file\GFG.txt"
try:
if os.path.exists(file_path):
os.chmod(file_path, 0o666)
print("File permissions modified successfully!")
else:
print("File not found:", file_path)
except PermissionError:
print("Permission denied: You don't have the necessary permissions to change the permissions of this file.")
with open(file_path, "w") as file:
new_content = "New content to replace the existing content."
file.write(new_content)
print("File content modified successfully!")
Output
File content modified successfully
Conclusion
In conclusion , To sum up, Error 13 is a frequent Python issue that arises from inadequate permissions when attempting to access certain files or folders. You may simply fix this problem and create reliable Python code by using the 'os' module, comprehending file permissions, and putting appropriate error handling in place.
Similar Reads
How to Fix - Reading A File: Permission Denied on Linux In this article, we will see how to fix when a permission error occurs while reading any file in Linux. We'll see how to fix that and also why that error occurs, and its common causes so that in future you will be able to solve those kinds of errors yourself. We'll learn various methods to solve thi
6 min read
How to Fix "NameError: name 'os' is not defined" in Python If we are working with Python and encounter an error that says "NameError: name 'os' is not defined", then we need not worry, it is a very common issue, and fixing this error is very simple. This error usually occurs when we try to use the os module without importing it first. In this article, we wi
3 min read
How to Fix - rm: Cannot Remove Directory/: Permission Denied In Linux, file removal, particularly directories, is a common task performed using the rm command. However, encountering a "Permission Denied" error can hinder this process, often due to insufficient permissions or system restrictions. Since, encountering the error "rm: Cannot Remove Directory/: Per
3 min read
How to Fix 'psycopg2.errors.insufficientprivilege' in Python When working with PostgreSQL databases in Python using the psycopg2 library, you might encounter the error psycopg2.errors.insufficientprivilege: permission denied for schema public. This error typically arises when a user attempts to perform an operation without the necessary permissions on the spe
4 min read
How to fix FileNotFoundError in Python FileNotFoundError is a built-in Python exception that is raised when an operation such as reading, writing or deleting is attempted on a file that does not exist at the specified path. It is a subclass of OSError and commonly occurs when functions like open(), os.remove() and similar file-handling m
2 min read