Python to print out status bar and percentage

后端 未结 20 1081
野的像风
野的像风 2020-11-28 01:01

To implement a status bar like below:

[==========                ]  45%
[================          ]  60%
[==========================] 100%

20条回答
  •  日久生厌
    2020-11-28 01:08

    There's a Python module that you can get from PyPI called progressbar that implements such functionality. If you don't mind adding a dependency, it's a good solution. Otherwise, go with one of the other answers.

    A simple example of how to use it:

    import progressbar
    from time import sleep
    bar = progressbar.ProgressBar(maxval=20, \
        widgets=[progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage()])
    bar.start()
    for i in xrange(20):
        bar.update(i+1)
        sleep(0.1)
    bar.finish()
    

    To install it, you can use easy_install progressbar, or pip install progressbar if you prefer pip.

提交回复
热议问题