Python script for checking file modification date

butterhook used Ask the Experts™
Is it possible that someone could help me with a script that will check a file and see if it's modification date is < X days ago, and if so return true?

I seem to be able to get this date about the file from os.stat, but I'm having trouble getting the current time in a suitable format for comparing.
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008
Try something like this:
import os,time
filename = 'YourFile.txt'
t = os.path.getmtime(filename)
daysold = (time.time() - t) / (60*60*24)
mod = time.strftime('%Y-%m-%d %H:%I:%S',time.localtime(t))
print mod,int(daysold),'days old'

Open in new window

please see here for examples on Python date manipulation


Thanks. Immediately usable.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial