Python (Scripting) - File Statistics

by
Jeremy Canfield |
Updated: August 25 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 | mode (file permissions) |
st_ino | the inode of the file |
st_dev | the device ID of the file |
st_nlink | number of hard links |
st_uid | user ID |
st_gid | group ID |
st_size | the size in bytes of the file |
st_atime | Access Time (the last time the file was accessed) |
st_mtime | Modifty Time (the last time the file was modified) |
st_ctime | Change Time (the last time the file was changed) |
This is similar to the Linux stat command.
~]# stat /tmp/foo.txt
File: /tmp/foo.txt
Size: 7940168 Blocks: 15520 IO Block: 4096 regular file
Device: fd04h/64772d Inode: 105729 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 409/ john.doe) Gid: ( 2001/admins)
Context: system_u:object_r:usr_t:s0
Access: 2020-03-31 22:44:47.895474745 -0500
Modify: 2020-03-31 22:43:17.434452999 -0500
Change: 2020-03-31 22:43:17.434452999 -0500
Birth: -
And here is how you can print each value.
#!/usr/bin/python3
import os
stat = os.stat("/path/to/example.txt")
print(f"mode = {stat.st_mode}")
print(f"inode = {stat.st_ino}")
print(f"device ID = {stat.st_dev}")
print(f"hard links = {stat.st_nlink}")
print(f"uid = {stat.st_uid}")
print(f"gid = {stat.st_gid}")
print(f"size = {stat.st_size}")
print(f"atime = {stat.st_atime}")
print(f"mtime = {stat.st_mtime}")
print(f"ctime = {stat.st_ctime}")
Or like this.
#!/usr/bin/python3
import os
print(f"{os.stat("/path/to/example.txt").st_ctime}")
Which should return something like this.
mode = 33188
inode = 8419830
device ID = 64768
hard links = 1
uid = 65234
gid = 100
size = 6
atime = 1724918963.7354586
mtime = 1724918963.7054596
ctime = 1724918963.727459
Notice that atime, mtime and ctime are epoch. Here is how you can convert atime, mtime and ctime to a human readable date and time.
#!/usr/bin/python3
import os
from datetime import datetime
stat = os.stat("/tmp/foo.txt")
atime = datetime.fromtimestamp(stat.st_atime)
ctime = datetime.fromtimestamp(stat.st_ctime)
mtime = datetime.fromtimestamp(stat.st_mtime)
print(f"atime = {atime}")
print(f"ctime = {ctime}")
print(f"mtime = {mtime}")
Which should return something like this.
atime = 2024-08-29 03:09:23.735459
ctime = 2024-08-29 03:09:23.727459
mtime = 2024-08-29 03:09:23.705460
strftime can be used if you want to adjust the format of the datetime object, often to drop the trailing seconds.
#!/usr/bin/python3
import os
from datetime import datetime
stat = os.stat("/tmp/foo.txt")
atime = datetime.fromtimestamp(stat.st_atime).strftime("%m/%d/%Y %H:%M:%S")
ctime = datetime.fromtimestamp(stat.st_ctime).strftime("%m/%d/%Y %H:%M:%S")
mtime = datetime.fromtimestamp(stat.st_mtime).strftime("%m/%d/%Y %H:%M:%S")
print(f"atime = {atime}")
print(f"ctime = {ctime}")
print(f"mtime = {mtime}")
Something like this should be returned. Now datetime_object no longer has the trailing seconds.
atime = 2024-08-29 03:09:23
ctime = 2024-08-29 03:09:23
mtime = 2024-08-29 03:09:23
To get the stats of a file on a remote system, you can use paramiko sftp.
#!/usr/bin/python3
import paramiko
host = "server1.example.com"
port = 22
username = "john.doe"
password = "itsasecret"
transport = paramiko.Transport((host,port))
transport.connect(None,username,password)
sftp = paramiko.SFTPClient.from_transport(transport)
stat = sftp.stat("/tmp/bar.txt")
print(f"info.st_atime = {info.st_atime}")
print(f"info.st_mtime = {info.st_mtime}")
if sftp:
sftp.close()
if transport:
transport.close()
Did you find this article helpful?
If so, consider buying me a coffee over at