The modern answer is pathlib, but the right choice depends on what you do next.
Aditya Challa1 min read
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().
exists() returns True for files and directories; is_file() and is_dir() narrow it down.
These work in every Python 3 version and accept strings.
Checking first and opening second leaves a race: the file can disappear in between. If you plan to read it, just try:
Lab evidence
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
Generators, lazily: what yield actually does to a function.
Summary12 Oct 20261 min read
Assignment expressions, explained with the examples that actually help.
Summary12 Oct 20261 min read
Use a timezone-aware datetime; utcnow() is deprecated since Python 3.12.
Summary12 Oct 20261 min read