python 进度条

import sys, time

class ProgressBar:
    def __init__(self, count = 0, total = 0, width = 100):
        self.count = count
        self.total = total
        self.width = width
    def move(self):
        self.count += 1
    def log(self, s):
        sys.stdout.write(' ' * (self.width + 12) + '\r')
        sys.stdout.flush()
        print s
        progress = self.width * self.count / self.total
        sys.stdout.write('{0:1}/{1:1}:'.format(self.count, self.total))
        
        sys.stdout.write('<' + '#' * progress + '-' * (self.width - progress) + '>'+'\r')
        if progress == self.width:
            sys.stdout.write('\n')
        sys.stdout.flush()
num = 20
bar = ProgressBar(total = num)
for i in range(num):
    bar.move()
    bar.log('We have arrived at: ' + str(i + 1))
    time.sleep(1)   
image.png
image.png
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容