October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run ScanOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk2 min

How to Check if a File Exists in Python

Use pathlib's exists() for any existing path and is_file() when you specifically need a regular file. Here are the os.path equivalents and key behavior differences.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Path("report.csv").exists() to check whether a path points to something that exists. It returns true for both files and directories. If you need to confirm that the path is a regular file, use Path("report.csv").is_file() instead.

Check whether a path exists with pathlib

pathlib is a clear choice for new Python code. Create a Path object, then call exists():

from pathlib import Path

path = Path("report.csv")
if path.exists():
    print("The path exists")

This check includes directories as well as files. For example, Path("reports").exists() can be true even if reports is a directory. The Python 3.14 pathlib documentation defines exists() as checking whether a path points to an existing file or directory.

Check that the path is a regular file

If a directory should not count, use is_file():

from pathlib import Path

if Path("report.csv").is_file():
    print("It is a regular file")

is_file() is the more precise predicate when your code needs a regular file rather than any existing path. It returns false for a directory.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose between pathlib and os.path

Python’s standard library provides procedural alternatives in os.path. Choose the predicate that matches what you mean:

What you need to check pathlib os.path
Any existing file or directory Path(path).exists() os.path.exists(path)
A regular file Path(path).is_file() os.path.isfile(path)

Use pathlib when you want to work with a Path object and continue using its path operations. Use os.path when the surrounding code already follows that style. The Python os.path documentation notes that isfile() follows symbolic links and accepts path-like objects.

Understand symbolic links and false results

By default, Path.exists() and Path.is_file() follow symbolic links: they test the link’s target. In Python 3.12 and later, Path.exists(follow_symlinks=False) checks whether the path itself exists without following a symbolic link.

A false result does not always mean that a path is simply absent. The Python 3.14 pathlib documentation says exists() returns false for invalid, inaccessible, or missing paths. If your program needs to tell those cases apart, call stat() and handle the filesystem errors it raises.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Account for Python version differences

Python 3.14 changed Path.exists(), Path.is_file(), and several related query methods: they now return false instead of raising operating-system OSError exceptions. Earlier Python versions could raise some errors and suppress others, so do not assume error behavior is identical across versions. Use stat() when the reason a lookup failed matters.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.