all Python 3statfunction
stat.S_ISCHR()
file type
Checks from st_mode that the object is a character device such as a terminal, /dev/null or /dev/random.
Syntax
stat.S_ISCHR(os.stat(path).st_mode)
Handy for finding out where standard input comes from: os.fstat(0).st_mode is a character device for a terminal and for /dev/null, a pipe (S_ISFIFO) for `prog | python x.py`, and a regular file (S_ISREG) for `python x.py < data.txt`. It cannot tell a terminal from /dev/null; use sys.stdin.isatty() for that.
Examples
import os, stat
print(stat.S_ISCHR(os.stat(os.devnull).st_mode)) # → True
open('a.txt', 'w').close()
print(stat.S_ISCHR(os.stat('a.txt').st_mode)) # → False
import os, stat
mode = os.fstat(0).st_mode # stdin
print(stat.S_ISCHR(mode), stat.S_ISFIFO(mode), stat.S_ISREG(mode)) # → ? зависит от того, откуда пришёл ввод
See also
Open in the full glossarydocs.python.orgReport an inaccuracy