Python Glossary Русский

all Python 3osfunction

os.stat

os — file system

Return an os.stat_result with a file's metadata.

Syntax

os.stat(path)

It follows symlinks: a broken link raises FileNotFoundError — use lstat to describe the link itself. Times are available with nanosecond precision in the *_ns fields. Permissions and type share st_mode, and the stat module decodes them: stat.S_ISDIR(st.st_mode), and stat.filemode(st.st_mode) gives a string like '-rw-r--r--'.

Examples

import os
print(os.stat(os.__file__).st_size > 0)   # → True
info = os.stat(os.__file__)
print(info.st_size == os.path.getsize(os.__file__))   # → True — os.path.getsize просто читает поле st_size
print(oct(info.st_mode & 0o777))   # → права файла восьмеричным числом, например 0o644 (на Windows обычно 0o666)
print(os.stat(os.__file__, follow_symlinks=False).st_size == os.lstat(os.__file__).st_size)   # → True — с follow_symlinks=False это ровно os.lstat
print(os.stat('нет-такого-файла.txt'))   # → FileNotFoundError

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy