Technical skill practice
Python Check If File Exists: exists, is_file and the Actual Read
To check whether a path exists in Python, use Path.exists. Use Path.is_file when the question is whether it is a regular file. A successful check is only an observation at that moment; the file can change or disappear before a later operation.
Start with a small, explicit task
Create a synthetic file inside a temporary directory, compare the file and directory checks, then remove the file between checking and reading. This controlled change demonstrates why the read itself still needs appropriate error handling. No existing user path is inspected or modified.
Run the complete example
This original example was checked with Python 3.12.14. Save it as python_check_file_exists.py and run it with Python 3.12. It uses only the standard library. The assertions check the stated results.
Expected output
directory exists but is not a file
prior check: True
read detected missing file
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory(prefix="path-check-") as folder:
directory = Path(folder)
path = directory / "sample.txt"
path.write_text("sample", encoding="utf-8")
assert directory.exists() and not directory.is_file()
assert path.exists() and path.is_file()
checked_before = path.exists()
path.unlink()
try:
path.read_text(encoding="utf-8")
except FileNotFoundError:
result = "read detected missing file"
else:
raise AssertionError("expected missing file")
assert checked_before and not path.exists()
print("directory exists but is not a file")
print("prior check:", checked_before)
print(result)
Explain the result
The directory passes exists but fails is_file, so existence alone is not enough when the application requires file content. The synthetic text file passes both. The script then deliberately deletes it before reading, turning a previously true existence result into a stale observation.
FileNotFoundError comes from the actual attempted read and addresses the state when that operation occurs. This is the important distinction: a preliminary check can inform a message or branch, but cannot reserve the file or prove a subsequent read will succeed. Other failures, such as insufficient permission, require their own handling based on the application.
Avoid these mistakes
Do not claim that is_file verifies readable content or a particular file format. It answers a path-type question. Keep exception handling close to the operation whose failure you can meaningfully address, and avoid swallowing all filesystem errors under a misleading “missing file” message.
Questions, answered
Does exists return true for directories?
Yes for the temporary directory checked here. Use is_file for the regular-file distinction.
Why catch FileNotFoundError after checking?
The example removes the file between operations, proving that the earlier result cannot guarantee the later read.
Sources and examples
ResumizeAI team. Examples are fictional teaching material, not customer results. Replace sample details with your own facts.
Your experience. A complete resume.
Build in Resumize with ATS-friendly templates, guided editing and a live preview. Start free, then upgrade when you need more.
Free account required. Free-plan PDF exports include a watermark.