Python Function Reference

os.path.exists

Check whether a file or directory exists at the given path.

Python 3.13
os.path.exists(path)
path
The path to the file or directory.
str | PathLike

Return value

True if the path exists, otherwise False.

Examples

Existing and missing file

Python 3.13
import os
open('notes.txt', 'w').close()
print(os.path.exists('notes.txt'))
print(os.path.exists('missing.txt'))
True
False

A directory exists too

Python 3.13
import os
os.makedirs('reports', exist_ok=True)
print(os.path.exists('reports'), os.path.isfile('reports'))
True False

Details

Files and directories are not distinguished; use os.path.isfile and os.path.isdir for that. Returns False for a broken symbolic link.

See also