Python Glossary Русский

Python 3.4+pathlibfunction

pathlib.Path.stat()

metadata

Returns an os.stat_result structure with the file's metadata: its size, modification time and permissions.

Syntax

p.stat()
p.stat().st_size
p.stat().st_mtime

stat() hits the filesystem on every call and raises FileNotFoundError when the path is gone, so store the result once instead of calling p.stat() again for each field. Sizes are in bytes and st_mtime is a float of seconds since the epoch — pass it to datetime.fromtimestamp() to get a date; the permission bits in st_mode mean little on Windows. On a symlink stat() reports the target — use lstat() to inspect the link itself.

Examples

from pathlib import Path
f = Path('stat_test.txt')
f.write_text('hello')
st = f.stat()
st.st_size # → 5
st.st_mtime # → float (unix timestamp)
import datetime
datetime.datetime.fromtimestamp(st.st_mtime) # → datetime объект
f.unlink()

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy