Skip to content
Latchkey

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

  1. Check path.is_file() before opening.
  2. When iterating a directory, skip entries that are not files.
  3. 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

Tired of flaky CI? Latchkey auto-heals failed jobs and retries them for you. Start free →