10/23/26About 1 minAbout 186 words
Concept: File stats
A path object allows us to retrieve information about the file it points to, such as its size and last modification time, using the stat method.
from pathlib import Path
log_file = Path("/var/log/nginx/access.log")
stats = log_file.stat()The stat method returns a os.stat_result object, which has a lot of attributes. The most useful ones are listed below:
Attributes
| Attribute | Description |
|---|---|
| st_mode | File type and permissions |
| st_ino | The inode number of Unix and the file index on Windows |
| st_dev | The device the file resides on |
| st_nlink | The number of hard links to the file |
| st_uid | The user ID of the file owner |
| st_gid | The group ID of the file owner |
| st_size | The size of the file in bytes |
Timestamps
| Attribute | Description |
|---|---|
| st_atime | The last access time of the file in seconds since epoch time |
| st_mtime | The last modification time of the file in seconds since epoch time |
| st_ctime | The most recent metadata change expressed in seconds |
| st_birthtime | The creation time of the file in seconds since epoch time |