all Python 3statfunction
stat.S_ISDIR()
file type
Checks, from the st_mode number returned by os.stat(), that the object is a directory. It takes a mode, not a path.
Syntax
stat.S_ISDIR(os.stat(path).st_mode)
A common mistake is passing a path: stat.S_ISDIR('data') raises TypeError; you need os.stat(path).st_mode. It pays off when you need several facts about a file: one os.stat() call and several S_IS* checks instead of a chain of os.path.isdir()/isfile(), each of which hits the file system again. os.stat() follows symbolic links, so a link to a directory also gives True.
Examples
import os, stat
os.mkdir('data')
open('notes.txt', 'w').close()
for name in ('data', 'notes.txt'):
mode = os.stat(name).st_mode
print(name, stat.S_ISDIR(mode)) # → data True / notes.txt False
import stat
try:
stat.S_ISDIR('data')
except TypeError as e:
print(type(e).__name__) # → TypeError — нужен st_mode, а не путь
See also
Open in the full glossarydocs.python.orgReport an inaccuracy