ENOTDIR
Linux / POSIXERRORCommonFile SystemHIGH confidence
Not a Directory
Production Risk
Common when path construction logic has a bug — a config value intended to be a directory path points to a file.
What this means
ENOTDIR (errno 20) is returned when a path component that is expected to be a directory is in fact a regular file or other non-directory. This commonly surfaces when a parent directory in a path is actually a file.
Why it happens
- 1A component of a path is a regular file, not a directory (e.g., /etc/hosts/foo where /etc/hosts is a file)
- 2Calling opendir() on a regular file
- 3Calling chdir() to a path that is not a directory
How to reproduce
Using a file as a directory component in a path.
trigger — this will error
trigger — this will error
import os
os.listdir('/etc/hosts/subdir')
# /etc/hosts is a file, not a directoryexpected output
NotADirectoryError: [Errno 20] Not a directory: '/etc/hosts/subdir'
Fix
Verify each path component is actually a directory
WHEN When building paths dynamically from user input or config
Verify each path component is actually a directory
import os
path = '/etc/hosts'
if os.path.isdir(path):
os.listdir(path)
else:
raise ValueError(f"{path} is not a directory")Why this works
Use os.path.isdir() before treating a path as a directory. In C, use stat() and check S_ISDIR(st.st_mode).
Sources
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev