制作Shell下面的进度条

This is a Python snippet, talking about console, progress bar, 进度条 and shell

制作Shell下面的进度条 Add to Favorite

def _flushWid2info(self):
        self._sql = "select count(*) as total from test"
        total = self._mysql.findOne(self._sql)['total']   #找到总数
       
        self._mysql.freeResult()
        self._sql = "select * from test"
        self._mysql.query(self._sql)
        _tmp = self._mysql.findNext()
        i = 0.0
        while _tmp:
            sys.stdout.write("%4d%% %d" % ((i / total) * 100, i) + "\b" * (6 + len(str(int(i))))) #相除得出进度 \b 就是退格
            _tmp = self._mysql.findNext()
            i += 1
        _tmp = None

和apt / yum的进度条一样~ 原理都是用 “b” 退格来完成

Created by ThePeppersStudio (364 days, 9.85 hours ago)

Do you want to leave a message? Please login first.