1、获取当前时间并格式化输出
import time t=time.gmtime() tplt='%Y-%m-%d %H:%M:%S' info=time.strftime(tplt,t) print(info)
2、程序计时应用:
2.1:测量时间:perf_counter()
import time start=time.perf_counter() end=time.perf_counter() interval=end-start print(interval)
2.2:休眠函数
def wait(): time.sleep(3.3)
调用main()函数后,程序休眠3.3秒后再接着运行
3、文本进度条
import time def progress_bar(): print('程序开始执行'.center(40,'-')) scale=10 for i in range(scale+1): a='-'*i c='.'*(scale-i) b=i/scale*100 print('{:>3.0f}%[{}->{}]'.format(b,a,c)) time.sleep(1) print('程序结束执行'.center(40,'-')) progress_bar()
3.1文件进度条单行进度刷新
import time def progress_bar(): start=time.perf_counter() scale=10 for i in range(scale+1): a='--'*i #c='.'*(scale-i) b=i/scale*100 interval=time.perf_counter()-start print(' {:>3.0f}%[{}>]{:3.2f}秒'.format(b,a,interval),end='') time.sleep(0.65) progress_bar()