File statistics : stat « Buildin Module « Python Tutorial






import os, sys
from stat import *
import datetime

sName = "test.txt"

if os.access( sName, os.F_OK ) == False :
    print "That file name doesn't exist!"
    exit()

sStat = os.stat( sName )
mMode= sStat[ST_MODE]
if S_ISDIR(mMode) :
    print "The path is a directory"
elif S_ISREG(mMode) :
    print "The path is a file"
else :
    print "I have no idea what the path is"


userID = sStat[ST_UID]
print "The user id that owns this file is: " + str(userID)

fSize = sStat[ST_SIZE]
print "The size of the file, in bytes, is: " + str(fSize)

accessTime = sStat[ST_ATIME]
print "The last access time is: " + str(accessTime)
print "As a date, this is: " + str(datetime.datetime.fromtimestamp(accessTime))

modTime = sStat[ST_MTIME]
print "The last modification time is: " + str(modTime)
print "As a date, this is: " + str(datetime.datetime.fromtimestamp(modTime))








14.13.stat
14.13.1.File statistics