Python script for checking file modification date

butterhook
butterhook used Ask the Experts™
on
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.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2008
Commented:
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

Author

Commented:
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