簡體   English   中英

如何在python中比較紀元時間和當前時間?

[英]How to compare the epoch time with current time in python?

我想比較創建的時間文件的輸出和實際時間

import os.path, time
print "created: %s" % time.ctime(os.path.getctime(file))
#Tue Apr 8 09:34:33 2014

print "created: %s" % os.path.getctime(file)
#1396965031

有可能做那樣的事情:

if os.path.getctime(file) < 100 #eg file older than 100 seconds
do something

謝謝

你應該能夠這樣做:

>>> created = os.path.getctime(filename)
>>> now = time.time()
>>> if now - created > 100:
    print "Haha"

或者簡短形式:

>>> if time.time() - os.path.getctime(filename) > 100:
    print "Haha"

在這兩種情況下,您都需要將文件的創建時間與當前時間進行比較。 另外, file是Python中的關鍵字。 所以盡量不要將它用作變量名。

希望這篇文章有所幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM