如何定期更新日期时间?

How to update datetime regularly?

我想每秒打印一次日期时间。当我运行

import time, datetime

t0 = time.time()

while True:
    print (time.strftime("%H:%M:%S",time.localtime(t0)))
    time.sleep(1)

我希望看到这个

20:58:30
20:58:31
20:58:32
20:58:33
20:58:34

等但我得到了这个:

20:58:30
20:58:30
20:58:30
20:58:30
20:58:30

我该怎么做才对?

import time, datetime

while True:
    t0 = time.time()
    print (time.strftime("%H:%M:%S",time.localtime(t0)))
    time.sleep(1)

如果您希望更新发生在同一行:

while True:
    sys.stdout.write("\r" + time.strftime("%H:%M:%S",time.localtime(time.time())))
    time.sleep(1)