简体   繁体   中英

How to compare the epoch time with current time in python?

I would like to compare the output of the created time file with the actual 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

Is possible to do somethng like that:

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

Thanks

You should be able to do like:

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

Or in short form:

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

In both cases, you need to compare the file's create time with the current time. In addition, file is a keyword in Python. So try NOT to use it as a variable name.

Hope the post be helpful!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM