Skip to content
Latchkey

Python "NotADirectoryError: [Errno 20]" in CI

NotADirectoryError: [Errno 20] Not a directory is raised when an operation expects a directory in a path but finds a regular file there, for example listing or traversing through a file.

What this error means

A step fails with "NotADirectoryError: [Errno 20] Not a directory: 'archive.zip/inner'" while listing or joining a path.

python
NotADirectoryError: [Errno 20] Not a directory: 'data.tar/contents'

Common causes

Treating an archive or file as a directory

Code appended a child path to something that is a file (a tar/zip), so traversal fails.

A name collision where a dir was expected

A previous step wrote a file with the same name the code now tries to enter as a folder.

How to fix it

Verify the path is a directory before traversing

  1. Check path.is_dir() before listing or joining child names.
  2. For archives, extract to a temp directory and traverse the extraction.
  3. Resolve name collisions so directories and files do not share a name.
Python
import pathlib
p = pathlib.Path(target)
if not p.is_dir():
    raise NotADirectoryError(f"expected a directory: {p}")

How to prevent it

  • Check is_dir() before directory operations.
  • Extract archives before traversing their contents.
  • Avoid file/dir name collisions in build output.

Related guides

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