我想检查一个文件是否比一定时间(例如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,但跨平台的解决方案会更好。谢谢!
我想检查一个文件是否比一定时间(例如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,但跨平台的解决方案会更好。谢谢!
我知道,这是一个老问题。但当我在寻找类似的东西时,我想到了这个替代方案:
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"
file
是什么? - Joe