Ad Space — Top Banner

FileExistsError

Python Programming Language

Severity: Minor

What Does This Error Mean?

A FileExistsError means your Python code tried to create a file or directory, but one with that name already exists at that path. Python will not silently overwrite it — it raises this error to prevent accidental data loss. The fix is to either check for the file before creating it, use a mode that allows overwriting, or delete the existing file first.

Affected Models

  • Python 3.12
  • Python 3.11
  • Python 3.10
  • Python 3.9
  • Python 3.8

Common Causes

  • Calling os.mkdir() or os.makedirs() on a directory that already exists
  • Using os.open() with the O_CREAT and O_EXCL flags on a file that already exists
  • Running the same script twice — the first run creates the file, the second fails
  • A previous run of the script was interrupted, leaving a partial file behind
  • Two processes running at the same time both trying to create the same file

How to Fix It

  1. If you are creating a directory with os.makedirs(), add the 'exist_ok=True' parameter. This tells Python to silently succeed if the directory already exists.

    Change 'os.makedirs(path)' to 'os.makedirs(path, exist_ok=True)'. This is the most common and cleanest fix for directory creation.

  2. If you are creating a file, check whether it already exists before creating it using os.path.exists() or pathlib's Path.exists().

    Example: 'if not os.path.exists(filepath): create the file'. Decide whether to skip creation, overwrite, or rename the new file.

  3. If you want to overwrite an existing file, open it in write mode ('w') rather than using exclusive creation mode. Write mode always creates or overwrites.

    Using open(filename, 'w') will create the file if it does not exist, or overwrite it if it does — no FileExistsError.

  4. If you need unique filenames, generate a new name when the target already exists — for example, by appending a number or timestamp.

    Example: 'output_1.txt', 'output_2.txt'. Use a loop that checks os.path.exists() until a free name is found.

  5. If a leftover file from a failed previous run is blocking you, delete it with os.remove() (for files) or shutil.rmtree() (for directories) and then proceed.

    Only do this if you are certain the old file is safe to delete. Check its contents first if there is any doubt.

When to Call a Professional

FileExistsError is straightforward to fix yourself in almost all cases. If you are building a concurrent system where multiple processes might create the same file simultaneously, consult a senior developer about file locking strategies. For production systems, the 'exist_ok' parameter and proper file existence checks are the standard solutions.

Frequently Asked Questions

Why does Python raise an error instead of just overwriting the file?

Python's default behavior protects you from accidentally destroying data. If you wrote a script that creates a backup file and ran it twice, silently overwriting the first backup would defeat the purpose. By raising an error, Python makes you consciously decide what to do — overwrite, skip, or rename.

What is the difference between os.mkdir() and os.makedirs()?

os.mkdir() creates a single directory and fails if it already exists. os.makedirs() creates the full path including all parent directories, and accepts the 'exist_ok=True' parameter. For most cases, os.makedirs(path, exist_ok=True) is the better and safer choice.

I deleted the file but still get FileExistsError — why?

Check that you deleted the right file at the right path. Print out the exact path your code is using (print(filepath)) and verify that path in your file explorer. Also check if a directory exists with the same name as the file you are trying to create — a directory and a file cannot share the same path.