Ad Space — Top Banner

NotADirectoryError

Python Programming Language

Severity: Moderate

What Does This Error Mean?

NotADirectoryError happens when your code tries to use a file path as if it were a folder, but it is actually a regular file. For example, you might try to list the contents of a path, or open a file inside a path — but that path points to a file, not a folder. The fix is to check that the path you are working with is actually a directory before treating it like one.

Affected Models

  • Python 3.x
  • Python 3.3 and later

Common Causes

  • Calling os.listdir() on a path that points to a file instead of a folder
  • Using os.makedirs() on a path where part of the path already exists as a file
  • Trying to open a file inside a path where part of the path is actually a file, not a folder
  • A variable meant to hold a directory path was accidentally set to a file path
  • A config file or user input provided a file path where a directory path was expected

How to Fix It

  1. Read the error message. It will show the path that caused the problem. Check whether that path points to a file or a folder on your system.

    In Windows File Explorer or Mac Finder, navigate to the path and see what is there — a file icon or a folder icon.

  2. Before using a path as a directory, check it with os.path.isdir(): if os.path.isdir(my_path): — only proceed if this returns True.

    os.path.isdir() returns False if the path does not exist at all, or if it exists but is a file. Both cases mean you cannot use it as a directory.

  3. If your code builds a file path by joining parts together, check each part to make sure none of the intermediate parts are files.

    Example: os.path.join('data', 'results', 'output.csv') — if 'data/results' is actually a file, not a folder, the join will point to something invalid.

  4. If the directory is supposed to exist, create it first with os.makedirs(my_path, exist_ok=True) — this creates the full path including any missing parent folders.

    exist_ok=True means it will not raise an error if the directory already exists.

  5. If the path comes from user input or a config file, validate it when it is received — check that it exists and is a directory before storing it for later use.

    Validating early gives a much clearer error message than letting the problem surface deep in your code.

When to Call a Professional

NotADirectoryError is always something you can fix yourself. The error message tells you exactly which path is the problem. Use os.path.isdir() to check that a path is a directory before treating it like one.

Frequently Asked Questions

What is the difference between NotADirectoryError and FileNotFoundError?

FileNotFoundError means the path does not exist at all. NotADirectoryError means the path does exist — but it is a file, not a directory. The distinction matters because the fix is different: FileNotFoundError means you need to create something, while NotADirectoryError means you have the wrong type of path.

Is NotADirectoryError a subclass of OSError?

Yes. NotADirectoryError is a subclass of OSError (formerly called EnvironmentError). You can catch it with 'except NotADirectoryError' for specific handling, or 'except OSError' to catch it along with other OS-level errors.

How do I check if something is a file vs a directory?

Use os.path.isfile(path) to check if it is a file, and os.path.isdir(path) to check if it is a directory. Both return True or False. You can also use pathlib: from pathlib import Path; p = Path(my_path); p.is_file() or p.is_dir() — pathlib is the modern way to work with file paths in Python 3.