Ste*_*ano 17 python comparison time filemtime
我想检查一个文件是否超过一定的时间(例如2天).
我设法以这样的方式获得文件创建时间:
>>> import os.path, time
>>> fileCreation = os.path.getctime(filePath)
>>> time.ctime(os.path.getctime(filePath))
'Mon Aug 22 14:20:38 2011'
Run Code Online (Sandbox Code Playgroud)
我现在如何检查这是否超过2天?
我在Linux下工作,但跨平台解决方案会更好.干杯!
Edu*_*rdo 22
我知道,这是一个老问题.但我正在寻找类似的东西,并想出了这个替代解决方案:
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."
Run Code Online (Sandbox Code Playgroud)
Eri*_*erg 21
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"
Run Code Online (Sandbox Code Playgroud)