Python Glossary Русский

all Python 3osterm

os.stat_result

os — file system

The result type of os.stat()/os.fstat(): has st_size, st_mode, st_mtime, etc.

Syntax

os.stat(path)  # → os.stat_result

It behaves both as an object with fields and as a ten-item tuple — the indices remain for compatibility, but fields read more clearly: st.st_size instead of st[6]. Integer seconds via indices versus float seconds via fields are a common source of confusion. Extra fields depend on the platform: st_birthtime, for example, is not available everywhere.

Examples

import os
print(isinstance(os.stat(os.__file__), os.stat_result))   # → True
res = os.stat(os.__file__)
print(res.st_size > 0, res.st_mtime > 0)   # → True True — размер в байтах и время последней записи в секундах эпохи
print(len(tuple(res)))   # → 10 — stat_result ведёт себя как кортеж из десяти классических полей
print(res[6] == res.st_size)   # → True — шестой элемент кортежной части и есть размер
res.st_size = 0   # → AttributeError: readonly attribute — поля stat_result неизменяемы

See also

Open in the full glossarydocs.python.orgReport an inaccuracy