python handwriting achieve progress bar

Ha ha ha, we are usually often see a progress bar of python, pip the installation package, update, call third-party library when ,,, but few people tried to achieve their own, today wrote to the interest on a ha ha ha sets

 

 

Output progress bar is refreshed in situ, so sleep (used output '\ r' and time library), '\ r' i.e. "wraps the cursor on the line." Here are two ways to provide:

---- use print ()

for i in range(0,101,2):
    print('\r----->>',i, '<<-----',end = "", flush=True)
  time.sleep(0.1)

---- use sys.stdout.write ()

import sys
_out = sys.stdout
for i in range(0,101,2):
  _out.write('\r'+'-->>'+str(i)+'<<--')
  time.sleep(0.1)

 

Next we have difficulty lifting points, plus the percentage of the progress bar graphics,

c = '#'
p = '_'
for iter in range(0,101,2):
    prc  = c* iter + p *(100-iter)
    print('\r',prc,'-->>' ,iter, '<<--', end='', flush=True)
    time.sleep(0.05)

 

Guess you like

Origin www.cnblogs.com/dynmi/p/12232027.html