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)
效果: