Python (Scripting) - File Statistics
by
Jeremy Canfield |
Updated: April 01 2024
| Python (Scripting) articles
os.stat can be used to return the stats of a file.
#!/usr/bin/python3
import os
print(os.stat("/path/to/example.txt"))
Something like this should be returned.
os.stat_result(st_mode=33188, st_ino=27901707, st_dev=64773, st_nlink=1, st_uid=65234, st_gid=100, st_size=0, st_atime=1701837284, st_mtime=1701837284, st_ctime=1701837284)
- st_mode = file permissions
- st_ino = file inode number
- st_dev = device identifier
- st_nlink = number of hard links
- st_uid = user owner
- st_gid = group owner
- st_size = file size
- st_atime = access time
- st_mtime = modified time
- st_ctime = create time
And here is how you can access a certain value in stat_result, such as st_ctime.
#!/usr/bin/python3
import os
stat = os.stat("/path/to/example.txt")
print(stat.st_ctime)
Or like this.
#!/usr/bin/python3
import os
print(f"{os.stat("/path/to/example.txt").st_ctime}")
Did you find this article helpful?
If so, consider buying me a coffee over at