pythontimecomparisonfilemtime

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(filePath)
>>> time.ctime(os.path.getctime(filePath))
'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!


Solution

  • 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"