How to Fix IOError: [Errno 13] Permission Denied in Python

Zeeshan Afridi Feb 02, 2024
  1. What Is the IOError: [Errno 13] Permission denied in File Handling in Python
  2. How to Fix the IOError: [Errno 13] Permission denied in Python
  3. How to Use the chmod Command to Change the Permissions of a File in Python
How to Fix IOError: [Errno 13] Permission Denied in Python

It is common to get IOError because we humans mostly interact with the GUI of a computer; we understand the difference between folders, files, compressed files or applications from the visuals.

We have interacted with folders, files, compressed files or applications so often that now our brains sometimes do not process to differentiate among these.

But on the other hand, you have to feed everything to computers they do not understand the visuals only; rather, you have to provide the complete details.

What Is the IOError: [Errno 13] Permission denied in File Handling in Python

In file handling, you need to provide the complete path of the file you want to access; otherwise, you will get the IOError. If you want to open a file but have provided the path of the folder instead, you will face the IOError: [Errno 13] Permission denied.

Below is a code example of the error in Python.

# opening file
f = open("E:\Projects\Test_folder", "r")

Output:

PermissionError: [Errno 13] Permission denied: 'E:\\Projects\\Test_folder'

In the above example, we are trying to open the Test_folder in reading mode, but this has thrown the PermissionError: [Errno 13] Permission denied.

How to Fix the IOError: [Errno 13] Permission denied in Python

To fix this, you need to enter the right path to the file you want to access, not the folder. Let’s say we have two files in the Test_folder.

import os

# Folder Path
folder_path = "E:\Client Project Report\Test_folder"

# display all files in a folder
print(f"All files in the Test_folder are\n{os.listdir(folder_path)}")

# file path
file_path = "E:\Client Project Report\Test_folder\Test_file_1.txt"

# read file
f = open(file_path, "r")
print(f"\n{f.read()}")

# file closed
f.close()

Output:

All files in the Test_folder are
['Test_file_1.txt', 'Test_file_2.txt']

Hi There!
This is test file 1

After providing the path of the file, the error is resolved. Also, it is a good practice to close the opened files in Python so no one can further read and write that file until and unless it is opened again, and if you are trying to write or read a closed file, it will raise a ValueError.

Although Python automatically closes a file when the reference object of the file is assigned to another file. But still, it is a good practice to close a file with the close() function.

In addition, you can also use the exception handling mechanism like try-catch blocks to catch such errors and keep your program safe from crashing.

How to Use the chmod Command to Change the Permissions of a File in Python

As discussed, the IOError: [Errno 13] Permission denied occurs when you try to open a file that is not permitted.

Let’s say you want to open a folder that is not allowed to your access, but still, you try to write a script in Python to open that folder, the permission to access the folder will be denied, and the Python compiler will throw the error.

To resolve this error, we can use the chmod command, which stands for change mode. The chmod() requires two arguments, the path of the file/folder you want to access and the file mode.

The chmod command is used to change the file permission of a file, and it is done by changing the permission flags of a particular file.

The permission flags are represented by a three-digit octal value used to specify read, write, and execute permissions for the file owner, the file group, and all other users.

Syntax of chmod:

chmod(path, mode)

The command takes two arguments:

  1. The first is the path to the file whose permissions you want to change.
  2. And the second is the permission you want to set.

Let’s say you want to give read and write permissions to everyone for a file named filename; you would use the following command.

chmod 777 filename

The Python script for the above command would be:

import os

os.chmod("my_file", 0o777)

This piece of code can also be represented as:

import os
import stat

path = "E:\Project\file1.txt"

# stat.S_IRWXU --> All permissions (Read, write, and execute) to the owner
# stat.S_IRWXG --> All permissions (Read, write, and execute) to group
# stat.S_IRWXO --> All permissions (Read, write, and execute) to others

print(os.chmod(path, stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO))

This would give everyone read, write, and execute permissions of the specific file.

What Does the Three Octal Number Represent in chmod

The permissions of a file can be represented using an octal number. The octal number is made up of three digits, each of which represents different permission.

  1. The first digit represents the owner’s permission.
  2. The second digit represents the group’s permissions.
  3. And the third digit represents the permissions for others.

The permissions of a file can be changed by using the chmod command with the octal number representing the desired permissions.

For example, to give the owner of a file read, write, and execute permissions while giving the group and others read and execute permissions only, the octal number 755 can be used.

Zeeshan Afridi avatar Zeeshan Afridi avatar

Zeeshan is a detail oriented software engineer that helps companies and individuals make their lives and easier with software solutions.

LinkedIn

Related Article - Python Error