exist
exception
file

How do I check whether a file exists without exceptions?

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Checking whether a file exists sounds simple, but the correct answer depends on what happens next. In Python, you can check existence without exceptions by using pathlib or os.path, but for critical workflows you still need to think about race conditions and whether you actually care about a file, a directory, or any path at all.

The Simplest Existence Check

The modern Python answer is Path.exists().

python
1from pathlib import Path
2
3path = Path("example.txt")
4
5if path.exists():
6    print("Path exists")
7else:
8    print("Path does not exist")

This check returns True for both files and directories. If all you want to know is whether some path exists, this is fine.

The older os.path.exists version works too:

python
1import os
2
3if os.path.exists("example.txt"):
4    print("Path exists")

Check Specifically for a Regular File

Many bugs happen because the code asks "does this path exist" when the real question is "is this a file".

python
1from pathlib import Path
2
3path = Path("example.txt")
4
5if path.is_file():
6    print("Regular file exists")
7else:
8    print("Missing or not a regular file")

If directories should be rejected, is_file() is the better choice.

Normalize the Path Before Checking

Sometimes the check fails because the path is relative, includes ~, or is being resolved from an unexpected working directory. Expanding and normalizing the path makes debugging much easier.

python
1from pathlib import Path
2
3raw = "~/data/input.csv"
4path = Path(raw).expanduser().resolve(strict=False)
5
6print("Checking:", path)
7print("Exists:", path.exists())

That does not change whether the file exists, but it makes the program's behavior clearer when paths come from user input or configuration.

Why Check-Then-Use Can Still Be Unsafe

This is the subtle part: a file can disappear after you check it and before you open it. Another process might delete it, replace it, or change permissions in that small time window.

That means this pattern is not truly safe for critical operations:

python
if path.exists():
    with path.open("r", encoding="utf-8") as file:
        data = file.read()

For robust code, the safer pattern is often to perform the operation directly and handle the failure if it occurs.

python
1from pathlib import Path
2
3path = Path("example.txt")
4
5try:
6    with path.open("r", encoding="utf-8") as file:
7        data = file.read()
8    print(data[:40])
9except FileNotFoundError:
10    print("File is missing")

That does use an exception, but it avoids the race condition.

Creating a File Only If It Does Not Exist

If the goal is to write a new file without overwriting an existing one, exclusive creation mode is better than a separate existence test.

python
1from pathlib import Path
2
3path = Path("report.txt")
4
5try:
6    with path.open("x", encoding="utf-8") as file:
7        file.write("new report\n")
8    print("Created")
9except FileExistsError:
10    print("Already exists")

This is atomic. It avoids the classic bug where two processes both check for existence, both see "missing," and both try to create the same file.

When a Plain Check Is Enough

A non-exception check is perfectly reasonable when:

  • you are showing a user a hint in the UI,
  • you are deciding whether to display an optional file,
  • the result is informational rather than security- or correctness-critical,
  • a race condition would not hurt anything important.

The mistake is using a plain existence check as if it guaranteed the next file operation will succeed.

Common Pitfalls

A common mistake is using exists() when the code really needs is_file(). A directory can exist too, and treating it like a file causes confusing failures later.

Another issue is forgetting that path checks are relative to the current working directory unless you normalize or resolve them. Many "missing file" bugs are actually "wrong directory" bugs.

Developers also often assume existence implies readability. A file may exist but still be blocked by permissions or locking.

Summary

  • Use Path.exists() or os.path.exists() for a simple non-exception existence check.
  • Use Path.is_file() when directories should not count.
  • Normalize paths early so diagnostics are clear.
  • For critical operations, prefer doing the file operation directly and handling failure.
  • Use exclusive creation mode instead of check-then-create when overwrites must be prevented.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.