Python 3.3+Exceptionsexception
NotADirectoryError
built-in exceptions
An operation expected a directory but got a non-directory (a subclass of OSError).
Syntax
raise NotADirectoryError
It shows up when a path that has to be a directory turns out to be a file: os.listdir('data.txt'), os.scandir on a file, or opening 'data.txt/inner.txt' where an intermediate component is a regular file. Check Path.is_dir() before the operation, and prefer catching the broader OSError, because the same situation can surface as a different subclass on another OS (FileNotFoundError, for instance).
Examples
try:
raise NotADirectoryError
except NotADirectoryError as e:
print(type(e).__name__) # → NotADirectoryError
See also
Open in the full glossarydocs.python.orgReport an inaccuracy