2014-02-21 102 views
0

我對此沒有太多瞭解,我試圖檢查一個文件的創建時間,並簡單地將日期(僅日,月和年)與今天的日期(同樣只是日,月和年)檢查日期的創建時間並與今天的日期進行比較?

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

任何人都知道我怎麼能轉換日期,然後覈對當天的日期?

+0

見http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python – joel3000

回答

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

這是相當多了,這​​不是很爲我工作但是它非常接近,我可以通過選擇前10個字符來修復它,但基本上我不需要比較文件的時間,只是日期 – Ryflex

相關問題