yield 打印进度条
def print_progress(total): received_size = 0 current_percent = 0 while received_size < total: new_size = yield if int((received_size / total) * 100) > current_percent: print("#", end="") current_percent = int((received_size / total) * 100) received_size += new_size x = 1000 received_size = 0 progress = print_progress(1000) progress.__next__() while x > received_size: received_size += 100 try: progress.send(100) except StopIteration as e: print("[100%]", end="")
改进:python sys.stdout.write 实现更好的输出效果
import sys, os import time for i in range(100): time.sleep(1) sys.stdout.write("\r File transfer progress :%2d percent complete!" % i)
http://www.cnblogs.com/hongfei/p/3982259.html