Python Glossary Русский

all Python 3statfunction

stat.S_ISREG()

file type

Checks from st_mode that the object is a regular file rather than a directory, device, pipe or socket.

Syntax

stat.S_ISREG(os.stat(path).st_mode)

"Not a directory" does not mean "regular file": os.devnull, a named pipe or a socket are not directories either, yet S_ISREG gives False for them, and reading a pipe as if it were a file can hang. os.path.isfile() works exactly this way, except that it calls os.stat() itself and quietly returns False for a missing path, whereas os.stat() raises FileNotFoundError.

Examples

import os, stat
with open('a.txt', 'w') as f:
    f.write('hi')
print(stat.S_ISREG(os.stat('a.txt').st_mode))  # → True
print(stat.S_ISREG(os.stat(os.devnull).st_mode))  # → False — это устройство, а не файл

See also

Open in the full glossarydocs.python.orgReport an inaccuracy