Python Glossary Русский

all Python 3osfunction

os.path.exists

os.path — existence and type checks

Check whether a path exists (following symlinks).

Syntax

os.path.exists(path)

The answer is stale as soon as you get it: the file can appear or vanish before the next line, so it is safer to open it and catch FileNotFoundError. False comes back both when the path is missing and when you lack permission to look into its directory — the cases are indistinguishable. It follows symlinks: a broken link reports False although the link itself is on disk. It also accepts an open file descriptor number.

Examples

import os
print(os.path.exists(os.__file__))   # → True
print(os.path.exists('/tmp'))  # → True
print(os.path.exists('/nonexistent'))  # → False

See also

Open in the full glossarydocs.python.orgReport an inaccuracy