Python 3.4+pathlibfunction
.exists()
checks
Check whether the path exists (file, directory or anything else); returns False instead of raising for a missing path.
Syntax
p.exists()
The answer is stale the moment you get it: the file can appear or disappear between the check and the open, so production code usually just opens the file and catches FileNotFoundError instead of asking first. The method follows symlinks — a broken link pointing nowhere reports False even though the link itself is on disk; use is_symlink(), or exists(follow_symlinks=False) since Python 3.12, to tell them apart.
Examples
from pathlib import Path
Path('/tmp').exists() # → True
Path('/nonexistent').exists() # → False
Path.cwd().exists() # → True (текущий каталог существует всегда)
Path('~/notes.txt').exists() # → False: тильда не раскрывается, нужен .expanduser()
Path.cwd().exists(), Path.cwd().is_file() # → (True, False)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy