10.9. Pathlib Stat

  • Path.stat() - Get file or directory status information

10.9.1. SetUp

>>> from pathlib import Path

10.9.2. Stat

$ stat -r /tmp/mydir/myfile.txt
16777234 74991501 0100644 1 501 0 0 23 1739435370 1739435370 1739435522 1739434616 4096 8 64 /tmp/mydirectory/myfile.txt
$ stat /tmp/mydir/myfile.txt
16777234 74991501 -rw-r--r-- 1 matt wheel 0 23 "Feb 13 09:29:30 2025" "Feb 13 09:29:30 2025" "Feb 13 09:32:02 2025" "Feb 13 09:16:56 2025" 4096 8 0x40 /tmp/mydirectory/myfile.txt
>>> myfile = Path('/tmp/mydir/myfile.txt')
>>> myfile.stat()
os.stat_result(st_mode=33188, st_ino=73966028, st_dev=16777234, st_nlink=1, st_uid=501, st_gid=20, st_size=8965, st_atime=1739174544, st_mtime=1739174544, st_ctime=1739174544)
>>> oct(33188)
'0o100644'
>>> from datetime import datetime, timezone
>>>
>>> datetime.fromtimestamp(1739174544, tz=timezone.utc)
datetime.datetime(2025, 2, 10, 8, 2, 24, tzinfo=datetime.timezone.utc)