EISDIR
Linux / POSIXERRORCommonFile SystemHIGH confidence
Is a Directory
Production Risk
Often surfaces when a config file path is accidentally set to a directory path.
What this means
EISDIR (errno 21) is returned when an operation that requires a regular file is performed on a directory — for example, attempting to open a directory for writing, or calling unlink() on a directory.
Why it happens
- 1Calling open() with O_WRONLY or O_RDWR on a directory
- 2Calling unlink() on a directory (must use rmdir() instead)
- 3A path meant to point to a file actually points to a directory
How to reproduce
Opening a directory path for writing.
trigger — this will error
trigger — this will error
// C: write to a directory
int fd = open("/etc", O_WRONLY);
// Returns -1, errno = EISDIRexpected output
open: Is a directory (EISDIR)
Fix
Check whether the path is a file or directory before opening
WHEN When the path comes from user input or configuration
Check whether the path is a file or directory before opening
import os, stat
path = '/etc'
if stat.S_ISREG(os.stat(path).st_mode):
with open(path, 'w') as f:
f.write('data')
else:
raise IsADirectoryError(f"{path} is a directory")Why this works
Verify the path is a regular file before attempting file operations. Use rmdir() (not unlink()) to remove directories.
Sources
Content generated with AI assistance and reviewed for accuracy. Found an error? hello@errcodes.dev