如何在python中比较纪元时间和当前时间?

时间:2014-04-10 01:02:27

标签: python time

我想将创建的时间文件的输出与实际时间

进行比较
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

由于

1 个答案:

答案 0 :(得分:2)

你应该能够这样做:

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

或简称:

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

在这两种情况下,您都需要将文件的创建时间与当前时间进行比较。此外,文件是Python中的关键字。所以尽量不要将它用作变量名。

希望这篇文章有所帮助!

相关问题