开发者

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 开发者_JAVA百科a cross platform solution would be better. Cheers!


I know, it is an old question. But I was looking for something similar and came up with this alternative solution:

from os import path
from datetime import datetime, timedelta

two_days_ago = datetime.now() - timedelta(days=2)
filetime = datetime.fromtimestamp(path.getctime(file_path))

if filetime < two_days_ago:
  print "File is more than two days old."


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜