Handling Error 13: Permission Denied in Python

Error 13 Permission Denied Python

Error 13: Permission Denied in Python is an I/O error that occurs when the system cannot communicate with your code to carry out the desired operation. Causes include trying to access a nonexistent file, an already-opened file, opening a directory as a file, or having incorrect permissions. There are multiple ways to fix this error, such as closing other instances of the file, updating permissions, ensuring you’re not accessing a directory, and using try-except blocks for error handling.

The error is very simple as shown below:

IOError: [Errno 13] Permission denied

An input/output error is a type of runtime error. Runtime errors are caused by wrong formats of input or when the code is unable to produce an output.

Since there are no syntax errors in the code, the Python interpreter interprets the code, compiles it, but when executing, it cannot parse particular statements or execute them.

Common Causes of I/O Errors (Permission Denied)

Input/output errors are caused by several reasons. Some of them are:

  • The most common cause of an I/O error is if the file specified doesn’t exist.
  • When trying to read() or open() a file, the system cannot communicate with the code if the file is already opened somewhere else.
  • If you’re trying to open a directory instead of a file, but trying to open it like a file, it will raise an input/output error.
  • If there is a forward slash in the path of the directory, it might also raise an i/o error.
  • If you don’t have permission to open that particular file or if Python doesn’t have access to the specific directory.

There is a very thorough article on Python i/o errors on ask python, you can check it out here!

Resolving Error 13: Permission Denied in Python

There can be more than one way by which this error can be fixed. They are:

  • Making sure you spell the file correctly and not using the path of a directory.
  • Making sure that the permissions are updated before trying to access the file.
  • The path of the file must be specified without any errors such as a forward slash.
  • The file that we’re trying to open shouldn’t be opened somewhere else.
  • Using a try and except block to find out where the error arises.

We’ll look at all these methods in depth one by one.

Fix 1: Close Other Instances of the File

When a particular file is opened in Microsoft excel or Microsoft word, this error frequently pops up. This happens because these applications do not let any other application modify a file when used via them.

Hence, make sure you have the files closed before trying to access them via your python code and make sure that on a windows computer no Microsoft office applications are using your file when you’re trying to open() or read() it.

Fix 2: Update Permissions and Run as Administrator

When trying to run your python script make sure you run it via the command prompt in the administrator mode.

This won’t affect your existing permissions for other applications, it will only override the Python permission for this particular script and no other. Make sure you’ve installed Python with the required PATH permissions.

When you open the command prompt, choose “run as administrator” from the right-hand panel as shown below in the picture with the red arrow.

Using Command Prompt In The Administrator Mode
Using Command Prompt In The Administrator Mode.

Fix 3: Ensure You Are Not Accessing a Directory

In this case, you’re trying to open a directory instead of trying to open a particular file. This problem can be fixed by specifying the absolute path of a file.

For example, if your file name is “First_program.txt” then you might as well use the command below with open().

open ('D:/SHREYA/documents/First_program.txt','w')

This helps in avoiding mistakes in specifying file names or paths and even directories.

Fix 4: Use Try and Except Blocks for Error Handling

The try and except statements can be used as well in this case. Try and except blocks are used to handle exceptions in Python. The code for the following would be:

try:
   open('Our_file.txt','r')
except IOError:
   print("The file cannot be opened")

We have an in-depth article about python exception handling. Click here to read!

Summary

Error 13: Permission Denied in Python can be addressed through various methods and understanding its causes. By applying these solutions, you can avoid hiccups in your programming journey and ensure smooth progress. Have you come across any other creative solutions or preventative measures to tackle permission errors?