Python 3.4+pathlibfunction
.is_file()
checks
Check that the path exists and points to a regular file; False for a directory or a missing path.
Syntax
p.is_file()
"Regular file" is meant literally: sockets, FIFOs and device nodes do exist, yet is_file() reports False for them. Symlinks are resolved, so a link to a file counts as a file. Don't branch on the negation — not p.is_file() does not imply a directory, it may simply be a path that isn't there.
Examples
from pathlib import Path
import sys
Path('/etc/hosts').is_file() # → True
Path('/tmp').is_file() # → False (это каталог)
print(Path(sys.executable).is_file()) # → True (сам интерпретатор — обычный файл)
print(Path(sys.executable).parent.is_file()) # → False (каталог, хотя exists() → True)
print(Path('no_such_file_42.txt').is_file()) # → False (несуществующий путь, а не исключение)
See also
Open in the full glossarydocs.python.orgReport an inaccuracy