我有一个每分钟运行一次的脚本,直到午夜。我希望它在工作进程时更新命令行。我希望脚本能够更新我执行此循环的计算次数,下一次循环将执行的次数以及下一个循环的持续时间。这是我的代码:
import datetime
import time
import sys
from crawler import crawler
seconds_for_sleep = 60
total_x = 0
total_y = 0
t = datetime.datetime.now()
while t < datetime.datetime( t.year, t.month, t.day+1,0,0,0,0):
x, y = crawler()
total_x +=x
total_y +=y
sys.stdout.write("\r")
sys.stdout.write("{:2d} calculations made in total.".format(total_x) )
sys.stdout.write("{:2d} calculations next cycle.".format(total_y) )
sys.stdout.flush()
for remaining in range(seconds_for_sleep, 0, -1):
sys.stdout.write("\r")
sys.stdout.write("{:2d} seconds remaining.".format(remaining))
sys.stdout.flush()
time.sleep(1)
sys.stdout.write("\rStarting Next Round! \n")
一个周期后的所需输出应为
10 calculations in total
6 calculations next cycle
然后它应该更新而不打印新行到
16 calculations in made in total
3 calculations next cycle
但是,正如我编写的脚本,它不会这样做。它反而只是一遍又一遍地清除同一条线。如何使用stdout
来完成我想要的工作?