Last updated: Apr 8, 2024
Reading timeยท3 min
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.
Here is an example of how the error occurs.
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.
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.
Note that I'm on Linux. If you are on Windows, the absolute path to your file will look similar to the following.
my_str = r'C:\Users\Bobby\Desktop\my-file.txt'
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.
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.
Alternatively, you can use an if
statement to check if the path points to a
file or a folder before opening it.
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.
If you meant to open all files in a directory, use a list comprehension to select the names of the files.
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.
path
argument is not provided, the method returns the entries for the current directory (the same directory as the Python script).If you need to recursively open all files in a directory, use the os.walk()
method.
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.
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.
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.
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).
my_str = r'/home/alice/Desktop/my-file.txt' my_str_2 = r'C:\Users\Alice\Desktop\my-file.txt'