ShopperCove
Menu
All writingBlogTopicsCategoriesAboutRSS
Blog
Categories
Observability & SRE62All categories
Topics
sre111localhost lab102performance70python58javascript47bundle size36benchmarking35node.js35web development21typescript19benchmark16content creation15All topics
About
Loading article…

ShopperCove

Hands-on notes on developer tools, written after the tool has actually been run.

All writingBlogCategoriesTopicsAboutPrivacyRSS

© 2026 ShopperCove

On this page

  1. Using pathlib (recommended)
  2. Using os.path
  3. Prefer try/except when you will open the file
  4. Sources
  1. Blog

How to Check if a File Exists in Python (3 Ways)?

The modern answer is pathlib, but the right choice depends on what you do next.

Aditya Challa·12 October 2026·1 min read

Summary

Disclosure

This post contains affiliate links. If you buy through them, I may earn a commission at no extra cost to you. I only make firsthand claims about products I have actually used.

On this page
  1. Using pathlib (recommended)
  2. Using os.path
  3. Prefer try/except when you will open the file
  4. Sources

Use pathlib.Path("data.txt").is_file() to check whether a regular file exists in Python. If you only care that something exists at the path (file or directory), use Path(...).exists() or os.path.exists().

Using pathlib (recommended)

from pathlib import Path

p = Path("data.txt")
if p.is_file():
    print("file exists")

exists() returns True for files and directories; is_file() and is_dir() narrow it down.

Using os.path

import os
os.path.isfile("data.txt")
os.path.exists("data.txt")

These work in every Python 3 version and accept strings.

Prefer try/except when you will open the file

Checking first and opening second leaves a race: the file can disappear in between. If you plan to read it, just try:

try:
    with open("data.txt") as f:
        text = f.read()
except FileNotFoundError:
    text = ""

Sources

  • 01docs.python.org/3/library/pathlib.html
  • 02docs.python.org/3/library/os.path.html
pythonpathlibos.pathfilenotfounderrorprogrammingfile handling

Lab evidence

What I found running this

Facts in this draft were checked on 12 Oct 2026 against the primary sources listed in the body (official docs, release schedules or Google/YouTube help pages). No benchmarks or timings were run, so the post makes no performance or measurement claims; dates are quoted as published by the maintainers, and any estimate is labelled as such.

Explore the product

Freelancers and devs who want to turn coding skills into paid site builds

Build ready-made client websites faster

Check out Ready Client Sites

Notes when a lab post goes up

Occasional email for new hands-on reviews. No sequence and no sponsors.

Explore the product

Freelancers and devs who want to turn coding skills into paid site builds

Build ready-made client websites faster

Check out Ready Client Sites

Keep reading

  • What Does yield Do in Python? Generators Explained

    Generators, lazily: what yield actually does to a function.

    Summary12 Oct 20261 min read

  • What Is the Walrus Operator (:=) in Python? Examples

    Assignment expressions, explained with the examples that actually help.

    Summary12 Oct 20261 min read

  • How Do I Get the Current UTC Time in Python Today?

    Use a timezone-aware datetime; utcnow() is deprecated since Python 3.12.

    Summary12 Oct 20261 min read