Python 3.4+pathlibfunction
.is_dir()
checks
Check that the path exists and points to a directory; False for a file or a missing path.
Syntax
p.is_dir()
A False answer merges two different situations — the path is missing, or it exists but is a file — so ask exists() separately when you need to tell them apart. Symlinks are followed: a link pointing at a directory counts as a directory. Also beware the check-then-act race; for real work it is safer to attempt the operation and handle OSError than to guard it with an if.
Examples
from pathlib import Path
Path('/tmp').is_dir() # → True
Path('/etc/hosts').is_dir() # → False (это файл)
Path.cwd().is_dir() # → True
Path('no_such_dir_12345').is_dir() # → False: несуществующий путь тоже даёт False, без исключения
[p.name for p in Path.cwd().iterdir() if p.is_dir()] # → список имён подкаталогов текущего каталога
See also
Open in the full glossarydocs.python.orgReport an inaccuracy