18

我想检查文件是否超过一定时间(例如 2 天)。

我设法以这种方式获得文件创建时间:

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

我现在如何检查这是否超过 2 天?

我在 Linux 下工作,但跨平台解决方案会更好。干杯!

4

2 回答 2

28

我知道,这是一个老问题。但我一直在寻找类似的东西并想出了这个替代解决方案:

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."
于 2013-08-08T12:58:00.517 回答
23
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"
于 2011-09-15T12:42:18.900 回答