Path.exists
exists() is True for anything; is_file() only for regular files; is_dir() only for folders. They follow symlinks unless you pass follow_symlinks=False (exists 3.12+, is_file / is_dir 3.13+). is_symlink, is_junction and is_mount inspect the link or the mount itself.
Common call
if p.is_file():
Returns
bool
Replaces
os.path.exists / isfile / isdir / islink / ismount / isjunction
Watch out
checking then acting is racy — try the operation and catch the error
Path.exists(*, follow_symlinksfollow_symlinks — exists (3.12+), is_file and is_dir (3.13+): with False, a symlink is judged by itself rather than its target.type: bool · default: True=True) / .is_file() / .is_dir() / .is_symlink() / .is_junction() / .is_mount()
→ bool
Demo
Live evaluation
Create a few files, then ask about one path.
Try:
Inputs
fileslist[str]files to create, comma-separated
querystrpath to check
Code
from pathlib import Path for f in ['config.toml', 'src/app.py']: Path(f).parent.mkdir(parents=True, exist_ok=True) Path(f).touch() p = Path('config.toml') (p.exists(), p.is_file(), p.is_dir())
Result
(True, True, False)
A missing path gives (False, False, False) — no exception. '.' is the (existing) current folder. is_file() and is_dir() are never both True; both are False for missing paths and for special files such as sockets or devices.
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| follow_symlinks | bool | no (True) | exists (3.12+), is_file and is_dir (3.13+): with False, a symlink is judged by itself rather than its target. |
Return value
bool — True or False — a missing path is False, not an exception.
Common patterns
Load a config file if present
The simple check is fine when nothing else changes the file.
from pathlib import Path cfg = Path('settings.toml') text = cfg.read_text(encoding='utf-8') if cfg.is_file() else ''
EAFP instead of check-then-act
Try it and handle the error — no race between check and use.
from pathlib import Path try: text = Path('settings.toml').read_text(encoding='utf-8') except FileNotFoundError: text = ''
Detect a dangling symlink
The link exists but its target does not.
from pathlib import Path def is_broken_link(p): return p.is_symlink() and not p.exists()
Examples
1. A file
from pathlib import Path
Path('a.txt').touch()
p = Path('a.txt')
(p.exists(), p.is_file(), p.is_dir())
Returns
(True, True, False)2. A folder
from pathlib import Path
Path('docs').mkdir()
p = Path('docs')
(p.exists(), p.is_file(), p.is_dir())
Returns
(True, False, True)3. Missing: False, not an error
from pathlib import Path
p = Path('nope')
(p.exists(), p.is_file(), p.is_dir(), p.is_symlink())
Returns
(False, False, False, False)4. A plain file is not a symlink
from pathlib import Path
Path('real.txt').touch()
Path('real.txt').is_symlink()
Returns
False5. An ordinary sub-folder is not a mount point
from pathlib import Path
Path('sub').mkdir()
Path('sub').is_mount()
Returns
False6. Junctions exist only on Windows
from pathlib import Path
Path('d').mkdir()
Path('d').is_junction()
Returns
False7. Special files
from pathlib import Path
p = Path('x.txt')
p.touch()
(p.is_socket(), p.is_fifo(), p.is_block_device(), p.is_char_device())
Returns
(False, False, False, False)Pitfalls
1. Check, then act
Another process can create or delete the file between exists() and the operation. Prefer doing it and catching the error — or flags like exist_ok / missing_ok.
exists() then mkdir()
from pathlib import Path p = Path('out') if not p.exists(): p.mkdir() p.is_dir()
True
mkdir(exist_ok=True)
from pathlib import Path Path('out').mkdir(exist_ok=True) Path('out').is_dir()
True
2. Using exists() when you need a file
exists() is True for folders too. If you are about to read the path, ask is_file().
exists()
from pathlib import Path Path('data.csv').mkdir() Path('data.csv').exists()
True
is_file()
from pathlib import Path Path('data.csv').mkdir() Path('data.csv').is_file()
False
When to use
Use it
- Branching on what kind of thing a path is
- Filtering iterdir / glob results (is_file, is_dir)
Reach for something else
- Guarding an operation that might race → just try it and catch FileNotFoundError / FileExistsError
- File size, times, permissions → stat()
Notes
CPython impl
exists, is_file, is_dir, is_symlink, is_socket, is_fifo, is_block_device, is_char_device come from PathBase (Lib/pathlib/_abc.py) and read stat().st_mode; Path overrides is_mount (os.path.ismount) and is_junction (os.path.isjunction)
Errors
A missing path, a path through a file ("file.txt/x") and some other OSErrors give False; permission problems may still raise
is_junction
Added in 3.12. Only Windows has junctions; always False elsewhere
is_mount
Windows support added in 3.12
Changed in 3.14
All these checks return False for any OSError from the OS
FAQ
Path('file.txt').is_file() — True only for an existing regular file. Path(p).exists() is True for folders as well.