版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mingjieshendu/article/details/81316474
1.环境
Win10 x64 + Python2.7.13 x64
2.目的
在控制台显示一个程序运行的状态,向开发者反馈程序运行正常与否。虽然Python的扩展模块tqdm可以动态跟踪程序的运行情况,网上也推荐了很多库,这里自定义一个小函数,比较简单。
3.解决方式
这里以7为一个打印周期,每次打印一个dot,打过7次以后,从头重新打印。
使用sys.stdout.write(‘.’)打印dot,sys.stdout.write(‘\r’)重新回到打印行首,通过sys.stdout.write(’ \r’)覆盖打印的dots,再回到行首,sys.stdout.flush()不断刷新输出缓冲区。
4.效果
5.代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# dispProcTest.py
# date:2018-07-31
import sys
import time
def dispProc(i):
if( not i % 7):
sys.stdout.write('\r') # return head of row
sys.stdout.write(' \r') # cover dots with spaces,return head of row
sys.stdout.write('.') # continue to print pots
sys.stdout.flush() # refresh
sys.stdout.write('start...\n')
for i in range(30):
dispProc(i)
time.sleep(0.3)
sys.stdout.write('\nfinish')