Skip to content
Latchkey

Python "FileExistsError: [Errno 17]" in CI

FileExistsError: [Errno 17] File exists is raised by os.mkdir, os.makedirs without exist_ok=True, an exclusive open ("x"), or os.symlink when the target path already exists.

What this error means

A step fails with "FileExistsError: [Errno 17] File exists: 'build/out'" when creating a directory, symlink, or exclusive file.

python
FileExistsError: [Errno 17] File exists: 'build/out'

Common causes

mkdir without exist_ok on a leftover directory

A re-run or cached workspace already contains the directory, and mkdir refuses to recreate it.

An exclusive-create open on an existing file

Opening with mode "x" requires the file to not exist; a leftover artifact triggers the error.

How to fix it

Make creation idempotent

  1. Use os.makedirs(path, exist_ok=True) for directories.
  2. Remove or overwrite an existing target before an exclusive create, or use mode "w".
  3. Clean stale output at the start of the step in cached workspaces.
Python
import os
os.makedirs("build/out", exist_ok=True)

How to prevent it

  • Prefer exist_ok=True and idempotent setup steps.
  • Clean build output before regenerating it.
  • Be deliberate about exclusive-create modes.

Related guides

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