python秒表,方便测试计时

  工作中简单用的,没有漂亮的UI,因为要保存时间做处理,所以存起了对应的时间数据。
  代码bug也有不少,欢迎交流。
#coding = 'utf-8'
import time
#保存计时的结果
file = "./result.txt"

print('按下回车开始计时,按下 Ctrl + C 暂停计时。再次按下 Ctrl + C 结束计时')
with open(file, "w") as file:
    while True:
        timesave = 0
        raw_input("") # 这里在最后结束时会报错,不用管它
        starttime = time.time()
        print('开始')
        try:
            while True:
                print('count:', round(time.time() - starttime, 0), 'sec')
                time.sleep(1)
        except KeyboardInterrupt:
            print('结束')
            endtime = time.time()
            timesave = round(endtime - starttime, 2)
            file.write(str(timesave) + "\n") #写时间进file,最后才会显示。
            print('total time is:', timesave,'secs')
        continue
posted @ 2020-06-04 17:26  hansenn  阅读(301)  评论(0编辑  收藏  举报