IsADirectoryError: [Errno 21] Is a directory in Python

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
3 min

banner

# 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)

using a directory as if it were a file

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.

# Specify the complete 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)

specifying path to file

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

Note that I'm on Linux. If you are on Windows, the absolute path to your file will look similar to the following.

shell
my_str = r'C:\Users\Bobby\Desktop\my-file.txt'

# Using a relative path if the file is in the same direcory

If the file you are interacting with is located in the same directory as your Python script (main.py in the example), you can directly pass the name of the file to the open() function.

main.py
import os # ๐Ÿ‘‡๏ธ Passing a filename to open() file_name = r'example.txt' print(os.path.isfile(file_name)) # ๐Ÿ‘‰๏ธ True with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines)

Make sure to include the extension after the filename.

The code sample above assumes that there is a file named example.txt in the same directory as your Python script.

# Checking if the path points to a file or a folder

Alternatively, you can use an if statement to check if the path points to a file or a folder before opening it.

main.py
import os file_name = r'/home/borislav/Desktop/bobbyhadz_python' print(os.path.isfile(file_name)) # ๐Ÿ‘‰๏ธ False print(os.path.isdir(file_name)) # ๐Ÿ‘‰๏ธ True if os.path.isfile(file_name): with open(file_name, 'r', encoding='utf-8') as f: lines = f.readlines() print(lines) else: # ๐Ÿ‘‡๏ธ This runs print('The specified path is a folder')

The os.path.isfile() method returns True if the path is an existing, regular file.

# Opening all of the files in the directory

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).

# Recursively opening all of the files in a directory

If you need to recursively open all files in a directory, use the os.walk() method.

main.py
import os dir_name = r'/home/borislav/Desktop/bobbyhadz_python' files_in_dir = [os.path.join(path, file) for path, dirs, files in os.walk(dir_name) for file in files] 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)

The os.path.join method joins one or more paths intelligently.

main.py
import os # ๐Ÿ‘‡๏ธ /home/bobbyhadz print(os.path.join('/', 'home', 'bobbyhadz'))

The os.walk() method generates the file names in a directory tree by walking the tree top-down.

The method returns a tuple containing 3 elements for each directory in the tree.

The tuple contains the directory path, directory names and filenames as elements.

# Opening a file that is located in the same directory

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'
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.

Copyright ยฉ 2024 Borislav Hadzhiev