Check whether a file exists in Python
Use pathlib for a path-status question, and handle the actual operation when permissions or concurrent changes determine whether a file can be read.
Use Path(path).is_file() when you need to know whether a path points to a regular file. Path.exists() and os.path.exists() also return true for directories. None of these checks guarantees that a subsequent read will succeed.
Choose the predicate that matches the question
from pathlib import Path
from os.path import exists
path = Path("/data/report.txt")
print(exists(path)) # Does the path resolve to an existing object?
print(path.exists())
print(path.is_file()) # Is its target a regular file?
print(path.is_dir()) # Is its target a directory?
pathlib has been in the standard library since Python 3.4. Its predicates follow symbolic links by default. A directory may exist while being unsuitable for read_text().
Error handling is version-dependent. In Python 3.14, these pathlib status predicates suppress operating-system OSError failures and return False; earlier versions can still raise some errors. A false result therefore does not always prove absence. Invalid argument types and other programming errors are a separate issue. Use stat() when you need the underlying error instead of a Boolean result.
Read directly when reading is the real task
Another process can remove, replace, or change permissions on the file between the check and the read. Handle the operation itself:
from pathlib import Path
path = Path("/data/report.txt")
try:
data = path.read_text(encoding="utf-8")
except FileNotFoundError:
data = None
This treats absence as an expected outcome. PermissionError, IsADirectoryError, decoding errors, and other failures remain visible unless the application has a specific policy for them.
For creating a new file without overwriting an existing one, use exclusive creation rather than “check, then write”:
with open("new-report.txt", "x", encoding="utf-8") as output:
output.write("Report created.\n")
Handle FileExistsError when an existing destination is expected. The exclusive-open operation makes the existence decision at creation time; a prior check does not.
FAQ
Why does a path beginning with ~ appear missing?
Python does not perform shell expansion automatically. Use Path("~/report.txt").expanduser() first.
What happens with a broken symbolic link?
A normal existence check follows the link and returns false when its target is missing. Path.is_symlink() checks whether the path is a link; os.path.lexists() also recognizes a broken link itself.
How should I write Windows paths?
On Windows, use Path("C:/data/report.txt") or a raw string such as Path(r"C:\data\report.txt"). A normal string can interpret backslashes as escapes. A Windows-looking path is not automatically a Windows filesystem path when code runs on another operating system.
References: Python pathlib, Python built-in functions, Python path operations.
Contact us
Join the community
to join the community
Try Guance
Start online and pay only for what you use.
Get startedChoose a Guance plan