python实现进度条和百分比同时显示
python中同时打印进度条和百分比
仅打印进度条:
import sys,time for i in range(100): sys.stdout.write('>') sys.stdout.flush() time.sleep(0.1)
效果:
打印百分比:
import sys,time for i in range(100): sys.stdout.write('\r%s%%'%(i+1)) sys.stdout.flush() time.sleep(0.1)
效果:
打印进度条和百分比
import sys,time for i in range(100): k = i + 1 str = '>'*i+' '*(100-k) sys.stdout.write('\r'+str+'[%s%%]'%(i+1)) sys.stdout.flush() time.sleep(0.1)
让进度条短一些:
import sys,time for i in range(100): k = i + 1 str = '>'*(i//2)+' '*((100-k)//2) sys.stdout.write('\r'+str+'[%s%%]'%(i+1)) sys.stdout.flush() time.sleep(0.1)
效果: