IsADirectoryError: [Errno 21] Is a directory (Fix)

IsADirectoryError: [Errno 21] Is a directory happens when your Python code tries to open, read, write, or otherwise use a folder path where Python expects a file path.

This is a common beginner error when working with open(), file paths, or folder loops. The fix is usually simple: check the path, confirm whether it points to a file or a directory, and use the right operation for that path.

Quick fix #

path = "data.txt"  # use a file path, not a folder path

with open(path, "r") as file:
    content = file.read()
    print(content)

This error usually means the path points to a folder instead of a file.

What this error means #

Python is telling you:

  • It expected a file path
  • The path you gave points to a directory (folder)
  • This often happens with open(), os.remove(), or similar file operations
  • A directory and a file are different things in Python and in your operating system

For example, this path is a directory:

path = "my_folder"

But this path is a file:

path = "my_folder/data.txt"

If you want to learn more about how file paths work, see working with file paths in Python.

Why this happens #

This error usually appears for one of these reasons:

  • You passed a folder name to open()
  • You built the path incorrectly
  • You used a variable that contains a directory path instead of a file path
  • You looped through directory names and tried to open each one as a file
  • You expected a file like data.txt but only used the parent folder path

Common causes #

  • Using open() with a folder path
  • Passing a directory to os.remove() or another file-only function
  • Forgetting to include the file name in the path
  • Incorrectly joining paths in loops
  • Reading items from os.listdir() without checking their type

Example that causes the error #

Here is a simple example:

with open("my_folder", "r") as file:
    content = file.read()

If "my_folder" is a real folder, Python raises:

IsADirectoryError: [Errno 21] Is a directory: 'my_folder'
Traceback (most recent call last):File "example.py", line 1, in <module>with open("my_folder", "r") as file:IsADirectoryError: [Errno 21] Is a directory: 'my_folder'Where it happened — file and lineWhat went wrong — the exception typeWhy — the detailed message
Read this bottom-up: the error names the directory you tried to open as a file.

Python cannot read a folder as if it were a text file.

The fix is to open a real file inside that folder:

with open("my_folder/data.txt", "r") as file:
    content = file.read()
    print(content)

If you need a refresher on file reading, see how to read a file in Python or the Python open() function explained.

How to fix it #

When you see this error, go through these checks:

  • Check the exact path stored in your variable
  • Make sure the path includes the file name and extension
  • Use os.path.isfile() before opening a path
  • Use os.path.isdir() if you want to work with folders
  • List files in the folder first, then open one file at a time

A safe pattern looks like this:

import os

path = "my_folder/data.txt"

if os.path.isfile(path):
    with open(path, "r") as file:
        print(file.read())
else:
    print("This path is not a file.")

This prevents your code from trying to open a directory as a file.

Fix by checking file vs directory #

When a path might be either a file or a folder, test it first.

import os

path = "my_folder"

print(os.path.isfile(path))  # True if it is a file
print(os.path.isdir(path))   # True if it is a directory

This helps because:

  • os.path.isfile(path) confirms it is a file
  • os.path.isdir(path) confirms it is a directory
  • You can choose the correct operation
  • It is especially useful when paths come from user input or loops

Example:

import os

path = "my_folder"

if os.path.isfile(path):
    with open(path, "r") as file:
        print(file.read())
elif os.path.isdir(path):
    print("This is a folder, not a file.")
else:
    print("The path does not exist.")

You can also check whether a path exists first with os.path.exists().

Fix when looping through folder contents #

This error often happens in loops.

A beginner might write code like this:

import os

folder = "my_folder"

for name in os.listdir(folder):
    with open(name, "r") as file:
        print(file.read())

There are two problems here:

  • name may be a directory, not a file
  • name is not joined to the folder path

A better version is:

import os

folder = "my_folder"

for name in os.listdir(folder):
    path = os.path.join(folder, name)

    if os.path.isfile(path):
        with open(path, "r") as file:
            print(file.read())
    else:
        print(f"Skipping directory: {path}")

This works better because:

  • Some folders contain subfolders as well as files
  • It does not assume every item from os.listdir() is a file
  • It joins the folder path and item name correctly
  • It skips directories or handles them separately

If you want to learn more, see os.listdir() explained.

Debugging steps #

If you are not sure why the error is happening, check the path before the failing line.

Useful debugging commands:

print(path)

import os
print(os.path.exists(path))
print(os.path.isfile(path))
print(os.path.isdir(path))
print(os.getcwd())
print(os.listdir(path))

Here is what each one helps you find:

  • print(path) shows the exact value being used
  • os.path.exists(path) tells you whether the path exists
  • os.path.isfile(path) tells you whether it is a file
  • os.path.isdir(path) tells you whether it is a folder
  • os.getcwd() shows the current working directory
  • os.listdir(path) shows the contents of a folder

A simple debugging example:

import os

path = "my_folder"

print("Path:", path)
print("Exists:", os.path.exists(path))
print("Is file:", os.path.isfile(path))
print("Is directory:", os.path.isdir(path))
print("Current working directory:", os.getcwd())

Use these steps to look for:

  • Missing file names
  • Wrong path joins
  • A folder path being used with open()
  • The script running in a different directory than you expected

This error is part of a larger group of file system errors.

  • FileNotFoundError happens when the path does not exist
  • PermissionError happens when the path exists but access is denied
  • NotADirectoryError happens when code expects a folder but gets a file
  • OSError is the broader error family that includes this error

Related pages:

FAQ #

What does IsADirectoryError mean in Python? #

It means your code used a folder path where Python expected a file path.

Why does open() say Is a directory? #

Because the path passed to open() points to a directory instead of a file.

How do I fix Errno 21 Is a directory? #

Use the full path to a real file, or check the path first with os.path.isfile().

How can I tell if a path is a file or folder? #

Use os.path.isfile(path) for files and os.path.isdir(path) for folders.

Can os.listdir() cause this error? #

Yes. If you loop through items and try to open every item, some may be directories.

See also #

Press Esc to close