Stefano Stefano - 4 months ago 11
Python Question

Comparing dates to check for old files

I want to check if a file is older than a certain amount of time (e.g. 2 days).

I managed to get the file creation time in such a way:

>>> import os.path, time
>>> fileCreation = os.path.getctime(file)
>>> file
>>> time.ctime(os.path.getctime(file))
'Mon Aug 22 14:20:38 2011'

How can I now check if this is older than 2 days?

I work under Linux, but a cross platform solution would be better. Cheers!

now = time.time()
twodays_ago = now - 60*60*24*2 # Number of seconds in two days
if fileCreation < twodays_ago:
    print "File is more than two days old"