Skip to main content

How to Solve "IsADirectoryError: [Errno 21] Is a directory" in Python

The IsADirectoryError: [Errno 21] Is a directory error in Python occurs when you attempt to perform a file operation (like open(), read(), write()) on a directory as if it were a file.

This guide explains the causes of this error, provides clear solutions, and outlines best practices to avoid it.

Understanding the Error

The IsADirectoryError is a subclass of OSError. It's raised when a file operation is requested on a directory. The most common cause is using a directory path with the open() function in a mode intended for files (like 'r', 'w', 'a'). The open() function is for opening files, not directories.

Common Causes and Solutions

Incorrect Path: Specifying a Directory Instead of a File

The most frequent cause is simply providing a directory path where a file path is expected:

import os

# 👇️ Path to a directory (this will cause the error)
directory_name = r'/home/tomnolan/Desktop/python'

# ⛔️ IsADirectoryError: [Errno 21] Is a directory: '/home/tomnolan/Desktop/python'
# with open(directory_name, 'r', encoding='utf-8') as f:
# lines = f.readlines()
# print(lines)

Solution: Provide the full path to the file, not just the directory:

import os

# Correct path to a FILE
file_name = r'/home/tomnolan/Desktop/python/example.txt'

with open(file_name, 'r', encoding='utf-8') as f:
lines = f.readlines()
print(lines)

Checking if a Path is a File or Directory

Before attempting to open a file, it's good practice to check if the path is actually a file using os.path.isfile() or os.path.isdir():

import os

file_name = r'/home/tomnolan/Desktop/python'

if os.path.isfile(file_name):
with open(file_name, 'r', encoding='utf-8') as f:
lines = f.readlines()
print(lines)
else:
print('The specified path is a folder or does not exist.')
# Output: The specified path is a folder or does not exist.
  • os.path.isfile(path): Returns True if path is an existing regular file.
  • os.path.isdir(path): Returns True if path is an existing directory.

Listing and Processing Files Within a Directory

If you intend to work with all files within a directory, iterate through the directory's contents using os.listdir():

import os

dir_name = r'/home/tomnolan/Desktop/python'
files_in_dir = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name, f))] # Creates list of files only, using os.path.join to generate the full path

for file_name in files_in_dir:
with open(file_name, 'r', encoding='utf-8') as f: # Open each file using a complete path.
# Process the file...
pass # Replace this with your processing code.
  • os.listdir(dir_name) lists all files and directories inside a directory.
  • Crucially, you must use os.path.join(dir_name, f) to create the full path to each file before passing it to open(). os.listdir() only returns filenames, not full paths.

Recursively Processing Files in a Directory and Subdirectories

To work with files in a directory and all its subdirectories, use os.walk():

import os

dir_name = r'/home/tomnolan/Desktop/python'
files_in_dir = [os.path.join(path, file) for path, dirs,
files in os.walk(dir_name) for file in files] # Join paths
print(files_in_dir)
  • os.walk() generates the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames).

  • We use list comprehension to iterate over all paths in the directory and join them with file names using the os.path.join() method.

Using pathlib for Path Manipulation

The pathlib module offers a more object-oriented way to work with paths, often making code cleaner:

import pathlib
import os # os still needed for isfile()

dir_name = r'/home/tomnolan/Desktop/python'

# List comprehension with a check if the path is file.
files_in_dir = [f for f in os.listdir(dir_name) if os.path.isfile(os.path.join(dir_name,f))]
print(files_in_dir)

Conclusion

The IsADirectoryError: [Errno 21] Is a directory error occurs when you treat a directory as a file. The most important fixes are:

  • Double-check your paths: Ensure you're providing the full path to a file, not a directory, to functions like open().
  • Use os.path.isfile() and os.path.isdir(): Before attempting to open a file, verify that it's actually a file.
  • Use os.listdir() or os.walk() to process files within directories: Don't try to open the directory itself. Iterate through its contents.
  • Use os.path.join(): Construct paths correctly for cross-platform compatibility.
  • Consider pathlib: For more complex path manipulations, pathlib often provides a cleaner interface.

By following these guidelines, you can avoid this error and write more robust file-handling code in Python.