比较时间-Python
我有一个从文件中读取时间的函数。我把它放入一个变量中。然后我从当前时间中减去这个值,大部分时间会给我一个值 我的问题是我不知道如何检查我附加到变量上的这个值是否大于20秒比较时间-Python,python,datetime,variables,math,Python,Datetime,Variables,Math,我有一个从文件中读取时间的函数。我把它放入一个变量中。然后我从当前时间中减去这个值,大部分时间会给我一个值 我的问题是我不知道如何检查我附加到变量上的这个值是否大于20秒 def numberchecker(): with open('timelog.txt', 'r') as myfile: data=myfile.read().replace('\n','') a = datetime.strptime(data,'%Y-%m-%d %H:%M:%S.%f'
def numberchecker():
with open('timelog.txt', 'r') as myfile:
data=myfile.read().replace('\n','')
a = datetime.strptime(data,'%Y-%m-%d %H:%M:%S.%f')
b = datetime.now()
c = b-a (This outputs for example: 0:00:16.657538)
d = (would like this to a number I set for example 25 seconds)
if c > d:
print ("blah blah")
你得到的区别是一个timedelta对象
您可以使用c.seconds来获得秒数。在错误答案中添加了注释。我这样做了,仍然得到了errorUse c.seconds>d而不是c>d当我给d赋值时,例如10,我得到了错误类型error:unorderable types:datetime.timedelta>Intd转换成timedelta:datetime.timedeltaseconds=d
if c.total_seconds() > datetime.timedelta(seconds=d):
print ("blah blah")