gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:50:33 24 4
gpt4 key购买 nike

我对 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

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

最佳答案

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

关于python - 如何在 time.sleep 函数后轮询当前时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30462202/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com