Calculate file age in Python.

I use a file’s age for many things, especially when it comes to monitoring servers with nagios. Every language has their own modules to do these things, so here is my quick example to calculate a file’s age with Python.

import os
from datetime import datetime
stat = os.stat('/etc/php.ini')
fileage = datetime.fromtimestamp(stat.st_mtime)
now = datetime.now()
delta = now - fileage
print 'File\'s age: ' + fileage
print 'Today\'s time: ' + now
print 'Delta time: ' + delta

I found this site to be of great help when first learning about time, dates, and more at http://pleac.sourceforge.net/pleac_python/datesandtimes.html

This entry was posted in Programming and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.