IsADirectoryError: [Errno 21] Is a directory in Python

avatar

Borislav Hadzhiev

Last updated: May 1, 2022

banner

Photo from Unsplash

IsADirectoryError: [Errno 21] Is a directory in Python #

The Python "IsADirectoryError: [Errno 21] Is a directory" occurs when we try to interact with a directory as if it were a file. To solve the error, provide the complete path to the file if trying to work on a file or select all of the files in the directory and use a for loop.

isadirectory errno 21 is a directory

Here is an example of how the error occurs.

main.py
import os # 👇️ path to a directory directory_name = r'/home/borislav/Desktop/bobbyhadz_python' print(os.path.isfile(directory_name)) # 👉️ False # ⛔️ IsADirectoryError: [Errno 21] Is a directory: '/home/borislav/Desktop/bobbyhadz_python' with open(directory_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

We tried to open a directory as if it were a file which caused the error.

The open() function expects the complete (relative or absolute) path to the file.

One way to solve the error is to specify the complete path to the file.

main.py
import os # 👇️ path to a file file_name = r'/home/borislav/Desktop/bobbyhadz_python/example.txt' print(os.path.isfile(file_name)) # 👉️ True with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

You can use the os.path.isfile() method to test whether the provided path is a regular file.

If you meant to open all files in a directory, use a list comprehension to select the names of the files.

main.py
import os dir_name = r'/home/borislav/Desktop/bobbyhadz_python' files_in_dir = [f for f in os.listdir(dir_name) if os.path.isfile(f)] print(files_in_dir) for file_name in files_in_dir: with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

List comprehensions are used to perform some operation for every element, or select a subset of elements that meet a condition.

The os.listdir method takes the path as an argument and returns a list containing the names of the entries in the directory for the specified path.

If the path argument is not provided, the method returns the entries for the current directory (the same directory as the Python script).

If you are trying to interact with a file that is located in the same directory as your Python script, you don't have to specify an absolute path.

main.py
with open('example.txt', 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

The example above assumes that there is a file called example.txt in the same directory as the Python script (main.py).

Alternatively, you can specify an absolute path to the file.

An absolute file that points to the file might look something like the following (depending on your operating system).

main.py
my_str = r'/home/alice/Desktop/my-file.txt' my_str_2 = r'C:\Users\Alice\Desktop\my-file.txt'

Conclusion #

The Python "IsADirectoryError: [Errno 21] Is a directory" occurs when we try to interact with a directory as if it were a file. To solve the error, provide the complete path to the file if trying to work on a file or select all of the files in the directory and use a for loop.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.