hello cc

python进度条

Posted on 2016-01-10 18:39  星际海盗  阅读(190)  评论(0)    收藏  举报

1. 还有点bug,得空再改改

用途:读取一个文件,每读一行显示一下当前的进度。

#!/usr/bin/env python

import os,sys,string
import time

def show_bar(j,sum_):
    num = j
    sum = sum_
    bar = "#"
    rate = float(num) / float(sum)
    if rate >= 0.01 and rate <= 1.0:
        rate_num = int(rate * 100)
        print '\r%d%% :' %(rate_num),
        for m in range(0, rate_num):
            os.write(1, bar)
        sys.stdout.flush()

if __name__ == '__main__':
    if len(sys.argv) == 1:
        print 'Usage:[%s] + list.txt' % sys.argv[0]
        sys.exit()
    try:
        x = 0
        target  = sys.argv[1]
        fd  =  open(target,'r')
        lin = fd.readlines()
        lin_num = lin.__len__()
        fd.close()
        for i in lin:
            x = x + 1
            time.sleep(0.005)
            show_bar(x,lin_num)
    except Exception, e:
pass

hello man