NotADirectoryError: Errno 20 Not a directory (Fix)

NotADirectoryError: [Errno 20] Not a directory happens when Python expects a folder path, but your code gives it a file path instead.

This usually appears with functions like os.listdir(), os.chdir(), and os.scandir(). These functions work with directories, not files.

Quick fix

Use os.path.isdir() before calling a function that expects a directory:

import os

path = "notes.txt"

if os.path.isdir(path):
    print(os.listdir(path))
else:
    print("This path is not a directory:", path)

What this does:

  • Checks whether path is a directory
  • Only calls os.listdir() if the path is a folder
  • Avoids the error when the path points to a file

If you need help understanding path types, see working with file paths in Python.

What this error means

This error means:

  • Python expected a directory path
  • You gave a file path or another non-directory path instead
  • A function like os.listdir() or os.chdir() cannot continue

In simple terms:

  • "documents/" is usually a directory path
  • "notes.txt" is usually a file path

If a function needs a folder, passing "notes.txt" will cause this error.

Why it happens

Common reasons include:

  • A filename is passed to a function that needs a folder
  • Part of the path points to a file, not a directory
  • A variable name suggests a folder, but actually stores a file path
  • The path was built incorrectly

This is especially common when:

  • You are joining paths by hand
  • You are using user input
  • You assume a path is a folder without checking it first

Example that causes the error

Suppose notes.txt is a real file in your current folder.

import os

print(os.listdir("notes.txt"))

Output:

Traceback (most recent call last):
  ...
NotADirectoryError: [Errno 20] Not a directory: 'notes.txt'

Why this fails

os.listdir() lists the contents of a directory.

It can do this:

import os

print(os.listdir("documents"))

But it cannot do this:

import os

print(os.listdir("notes.txt"))

Because notes.txt is a file, not a folder.

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

How to fix it

There are several ways to fix this error.

Pass the correct folder path

If you meant to list a folder, pass the folder path:

import os

folder_path = "documents"
print(os.listdir(folder_path))

Check the path before using it

import os

path = "notes.txt"

if os.path.isdir(path):
    print(os.listdir(path))
else:
    print("Expected a directory, but got:", path)

This is one of the safest beginner-friendly fixes.

You can also use os.path.exists() explained to check whether the path exists at all.

Use open() for files and os.listdir() for folders

If your path is a file, use open() instead of os.listdir():

path = "notes.txt"

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

print(content)

Use:

  • open() for files
  • os.listdir() for folders

If needed, read more about the Python open() function.

A very common problem is that the variable contains something different from what you expected.

import os

path = "notes.txt"
print("Path value:", path)

if os.path.isdir(path):
    print(os.listdir(path))
else:
    print("Not a directory")

Printing the path often helps you spot:

  • wrong filenames
  • missing folder names
  • incorrect path building
  • unexpected user input

File path vs directory path

Understanding this difference helps prevent the error.

File path

A file path points to one file:

"data.txt"
"images/photo.jpg"

You usually use file paths with functions like:

  • open()
  • os.path.isfile()

Directory path

A directory path points to a folder:

"documents"
"images"
"projects/python"

You usually use directory paths with functions like:

  • os.listdir()
  • os.chdir()
  • os.scandir()

Different Python functions expect different path types. If you mix them up, errors happen.

Beginner debugging steps

If you get this error, check these things in order.

1. Print the path

print(path)

Make sure the value is really what you think it is.

2. Check whether the path exists

import os

print(os.path.exists(path))

If it returns False, the path is wrong or missing.

3. Check whether it is a file or directory

import os

print(os.path.isfile(path))
print(os.path.isdir(path))

This tells you what the path actually points to.

4. Check your current working directory

Sometimes the path is relative, and Python is looking in a different place than you expect.

import os

print(os.getcwd())

5. Make sure you are using the right function

  • If the path is a file, use open()
  • If the path is a folder, use os.listdir() or another directory function

If you need a full overview of these tools, see the Python os module overview.

Common places this appears

You will often see this error in code like:

  • os.listdir(path)
  • os.chdir(path)
  • os.scandir(path)
  • Any code that expects a folder but receives a file path

For example, this will also fail if report.txt is a file:

import os

os.chdir("report.txt")

And this can fail when part of the path is a file:

import os

path = "notes.txt/archive"
print(os.listdir(path))

If notes.txt is a file, Python cannot treat it like a folder containing archive.

Common mistakes

Common causes of NotADirectoryError include:

  • Calling os.listdir() on a file
  • Calling os.chdir() with a filename
  • Joining paths incorrectly
  • Confusing a file path variable with a folder path variable
  • Using user input without checking whether it is a directory

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

FAQ

What is the difference between NotADirectoryError and FileNotFoundError?

NotADirectoryError means the path exists, but it is not a folder.

FileNotFoundError means the path does not exist.

For that related case, see FileNotFoundError: Errno 2 No such file or directory.

Can a file path cause NotADirectoryError?

Yes. If a function expects a directory and you pass a file path, Python raises this error.

How do I check if a path is a directory?

Use os.path.isdir(path).

Example:

import os

path = "documents"
print(os.path.isdir(path))

It returns:

  • True if the path is a directory
  • False otherwise

Should I use open() or os.listdir()?

Use:

  • open() for files
  • os.listdir() for folders

Using the wrong one can cause errors like this one or the related IsADirectoryError: Errno 21 Is a directory.

See also