检查创建的日期时间并与今天的日期进行比较?

时间:2014-02-21 01:09:05

标签: python datetime python-2.7 ctime

我对此并不是很远,我试图检查文件的创建时间,只是将日期(仅日,月和年)与今天的日期进行比较(再次只有一天) ,月份和年份)

import os.path, time
import datetime
fulldate = time.ctime(os.path.getctime("file.xlsx"))

任何人都知道我如何转换日期然后检查今天的日期?

1 个答案:

答案 0 :(得分:1)

import os.path, time
from datetime import datetime
from time import mktime

fulldate = time.ctime(os.path.getctime("file.xlsx"))
struct = time.strptime(fulldate)
filetime = datetime.fromtimestamp(mktime(struct))
filedate = filetime.replace(hour=0, minute=0, second=0, microsecond=0)

if filetime < datetime.now():
  print "The file is quite old"
else:
  print "The file is not so old"