本文實例講述了Python調用命令行進度條的方法。分享給大家供大家參考。具體分析如下:
關鍵點是輸出'\r'這個字符可以使光標回到一行的開頭,這時輸出其它內容就會將原內容覆蓋。
1
2
3
4
5
6
7
8
9
10
11
|
import time import sys def progress_test(): bar_length = 20 for percent in xrange ( 0 , 100 ): hashes = '#' * int (percent / 100.0 * bar_length) spaces = ' ' * (bar_length - len (hashes)) sys.stdout.write( "\rPercent: [%s] %d%%" % (hashes + spaces, percent)) sys.stdout.flush() time.sleep( 1 ) progress_test() |
希望本文所述對大家的Python程序設計有所幫助。