Python Glossary Русский

all Python 3statterm

stat

module

Constants and functions for decoding the file mode from os.stat: file type and permissions.

Syntax

import stat

st_mode from os.stat is an integer packing the file type and permissions; stat decodes it: S_ISDIR, S_ISREG, S_ISLNK, permission masks such as S_IRUSR, and filemode for the ls -l look. For a plain is-this-a-directory question, pathlib.Path.is_dir is shorter. Windows barely reflects permissions: it only distinguishes read-only.

Examples

import os, stat
open('f.txt', 'w').close()
mode = os.stat('f.txt').st_mode
print(stat.S_ISREG(mode), stat.S_ISDIR(mode))  # → True False
print(stat.filemode(stat.S_IFDIR | 0o755))  # → drwxr-xr-x

See also

Open in the full glossarydocs.python.orgReport an inaccuracy