1. 程式人生 > >python 迴圈中顯示進度條

python 迴圈中顯示進度條

import sys


# 迴圈時顯示進度條
# total 代表迴圈總數 ,num為當前迴圈數

def view_bar(num, total):
    rate = float(num) / float(total)
    rate_num = int(rate * 100)
    r = '\r[%s%s]%d%%,%d' % ("="*rate_num, " "*(100-rate_num), rate_num, num )
    sys.stdout.write(r)
    sys.stdout.flush()
import time
for i in range(100
): time.sleep(1) view_bar(i , 100) ''' [========= ]9%,9 '''