如何在time.sleep函数之后轮询当前时间

时间:2015-05-26 15:00:39

标签: python

我有一个Python函数的问题,作为一个菜鸟我不确定如何继续。

#!/usr/bin/python
 import time
 import ephem
 import datetime

 now_l = now.time()
 print("Time at Start of script")
 print now_l
 sleep_time = 600
 time.sleep(sleep_time)
 now_l = now.time()
 print("Time at after Sleep")
 print now_l

当我运行脚本时,结果是

Time at Start of script
16:38:59.210033
Sleeping for 600 seconds
Time at after Sleep
16:38:59.210033

我需要以相同的格式查询睡眠期后的当前时间。我做错了什么?

1 个答案:

答案 0 :(得分:1)

from time import sleep
from datetime import datetime

now_l = datetime.now().time()
print("Time at Start of script")
print now_l
sleep_time = 2
sleep(sleep_time)
now_l = datetime.now().time()
print("Time at after Sleep")
print now_l

abowe代码的输出是:

Time at Start of script
16:10:52.136008
Time at after Sleep
16:10:54.138556
相关问题