In this tutorial, we will discuss PermissionError
error in Python. This error occurs when a program tries to perform an operation for which it does not have appropriate permissions to access the specified file, directory or resource.
Causes of PermissionError
The PermissionError usually occurs because of the following reasons:
- Read-only File: When the file is read-only and we try to write on it, this error occurs.
- Permission Denied: It occurs when the user doesn’t have necessary permissions to perform the operation.
- Wrong-path: If the path is wrong, or the mentioned directory doesn’t have the required file, then also this error occurs.
- Locked files: Some operating systems place a lock on files while they’re being accessed, and if another program or process tries to access the file during this time, a permissions error may occur.
Examples
Let’s look into some examples to understand the problem better.
Example 1: Writing to a read-only file
try: file = open("text.txt", "r") file.write("Hello, Python!") except PermissionError as error: print("PermissionError:", error)
Output
If we try to write into a “read-only file” using write
method, it will prompt PermissionError
with a message.
PermissionError: [Errno 13] Permission denied: 'text.txt'
Example 2: Read/Write a file without necessary permissions
try: file = open("/etc/shadow", "w") file.write("Hello, Python!") except PermissionError as error: print("PermissionError:", error)
Output
If we try to read or write a file for which we don’t have sufficient read/write permissions, it will cause a PermissionError
with message.
PermissionError: [Errno 13] Permission denied: '/etc/shadow'
Example 3: Accessing Locked Files
try: file = open("file.txt", "r") file.write("Hello, Python") except PermissionError as error: print("PermissionError:", error)
Output
If we try to access locked files that are under system control, in use by other programs or are not accessible, we get a PermissionError
as below.
PermissionError: [Errno 13] Permission denied: 'file.txt'
Handling PermissionError exceptions
When we have PermissionError
we should allow the user to know that the appropriate permissions are not granted. Deping on the scripts we are running, in some cases, it is better to try again later when the file becomes free or simply terminate the application if the file is essential for execution.
Example 4: Handling Exception
In this example, the program waits for some seconds and then attempts to execute once more. This will give an opportunity for the file to be released by another process.
import time # Attempt to write to a read-only file. try: file = open('file.txt', 'r') time.sleep(5) file.write('Hello, Python!') file.close() # Handle permission denied exception. except PermissionError as error: print('PermissionError: {0}'.format(str(error))) time.sleep(5) file = open('file.txt', 'w') file.write('Hello, Python!') file.close()
Output
PermissionError: [Errno 13] Permission denied: 'file.txt'
The script waits for five seconds and then tries again to open the file only if possible. In some cases, it may be more desirable to terminate the script if the file is not available for manipulation.
Conclusion
Python’s PermissionError
is a common error that occurs when the program doesn’t have the required permissions to access a specified file, directory, or resource. This error can occur due to various reasons but most common among them are Read-only File, Permission Denied, Wrong-path, and Locked files. Handling these exceptions is crucial since they can have severe repercussions otherwise.