Python "IsADirectoryError: [Errno 21]" in CI
IsADirectoryError: [Errno 21] Is a directory is raised when an operation that expects a regular file (open for read, read bytes) is given a directory path.
What this error means
A step fails with "IsADirectoryError: [Errno 21] Is a directory: '/path/to/thing'" inside an open() or read call.
python
IsADirectoryError: [Errno 21] Is a directory: 'reports'Common causes
A glob or join produced a directory path
A path that is a folder was passed where a file was expected, often from an unfiltered listdir or glob.
A missing filename appended to a directory
Code built a path without the file component and opened the directory itself.
How to fix it
Filter to files before opening
- Check
path.is_file()before opening. - When iterating a directory, skip entries that are not files.
- Ensure the path includes the intended filename, not just the folder.
Python
import pathlib
for p in pathlib.Path("reports").iterdir():
if p.is_file():
data = p.read_text()How to prevent it
- Guard file operations with is_file() checks.
- Build paths explicitly including the filename.
- Use pathlib iterdir/glob filters to skip directories.
Related guides
Python "NotADirectoryError: [Errno 20]" in CIFix "NotADirectoryError: [Errno 20] Not a directory" in CI - a path component that the code treated as a dire…
Python "FileExistsError: [Errno 17]" in CIFix "FileExistsError: [Errno 17] File exists" in CI - mkdir or an exclusive open failed because the target al…
Python "FileNotFoundError" opening a missing path in CIFix "FileNotFoundError: [Errno 2] No such file or directory" in CI - code opened a path that does not exist o…