File Information (Python)

vegaseat 0 Tallied Votes 2K Views Share

This is a short code example using the function os.stat() to get some of the basic file information. Things like file size, times the file was created (Windows), last modified, and last accessed. The program uses a dictionary in an interesting way to store and display the information. The times are displayed in the 12 hour Windows file manager format (US), but you can easily change the formatting string to your local preferences.

# get file information using os.stat()
# tested with Python24    vegsaeat    25sep2006

import os
import stat  # index constants for os.stat()
import time

# pick a file you have ...
file_name = 'test.txt'
file_stats = os.stat(file_name)

# create a dictionary to hold file info
file_info = {
   'fname': file_name,
   'fsize': file_stats [stat.ST_SIZE],
   'f_lm': time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_MTIME])),
   'f_la': time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_ATIME])),
   'f_ct': time.strftime("%m/%d/%Y %I:%M:%S %p",time.localtime(file_stats[stat.ST_CTIME]))


print "file name     = %(fname)s" % file_info
print "file size     = %(fsize)s bytes" % file_info
print "last modified = %(f_lm)s" % file_info
print "last accessed = %(f_la)s" % file_info
print "creation time = %(f_ct)s" % file_info


if stat.S_ISDIR(file_stats[stat.ST_MODE]):
   print "This a directory"
   print "This is not a directory"


print "A closer look at the os.stat(%s) tuple:" % file_name
print file_stats
print "The above tuple has the following sequence:"
print """st_mode (protection bits), st_ino (inode number),
st_dev (device), st_nlink (number of hard links),
st_uid (user ID of owner), st_gid (group ID of owner),
st_size (file size, bytes), st_atime (last access time, seconds since epoch),
st_mtime (last modification time), st_ctime (time of creation, Windows)"""