1. 程式人生 > >Python3 Print 同一行列印顯示進度條效果

Python3 Print 同一行列印顯示進度條效果

# -*- coding:utf-8 -*-

'''
print 中的 end = '\r' 是一個轉義符,作用是讓游標重新回到首行
預設是'\n'換行符,windows系統下如果設定後不改回預設值會一直有效,不懂為什麼

'''

import sys,time

# 變數
total = 153

for i in range(total):

	if i+1 == total:
		percent = 100.0
		print('當前核算進度 : %s [%d/%d]'%(str(percent)+'%',i+1,total),end='\n')
	else:
		percent = round(1.0 * i / total * 100,2)
		print('當前核算進度 : %s [%d/%d]'%(str(percent)+'%',i+1,total),end='\r')
	time.sleep(0.01)

 實現效果: