Home >Backend Development >Python Tutorial >python在控制台输出进度条的方法

python在控制台输出进度条的方法

WBOY
WBOYOriginal
2016-06-06 11:18:381966browse

本文实例讲述了python在控制台输出进度条的方法。分享给大家供大家参考。具体实现方法如下:

进度条效果如下所示:

|#############################---------------------|
59 percent done

代码如下:

class ProgressBar():
  def __init__(self, width=50):
    self.pointer = 0
    self.width = width
  def __call__(self,x):
     # x in percent
     self.pointer = int(self.width*(x/100.0))
     return "|" + "#"*self.pointer + "-"*(self.width-self.pointer)+\
        "|\n %d percent done" % int(x)

Test function (for windows system, change "clear" into "CLS"):

if __name__ == '__main__':
  import time, os
  pb = ProgressBar()
  for i in range(101):
    os.system('clear')
    print pb(i)
    time.sleep(0.1)

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn