Python Progress Bar

后端 未结 30 2618
礼貌的吻别
礼貌的吻别 2020-11-22 06:13

How do I use a progress bar when my script is doing some task that is likely to take time?

For example, a function which takes some time to complete and returns

30条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-22 06:55

    A very simple approach:

    def progbar(count: int) -> None:
        for i in range(count):
            print(f"[{i*'#'}{(count-1-i)*' '}] - {i+1}/{count}", end="\r")
            yield i
        print('\n')
    

    And the usage:

    from time import sleep
    
    for i in progbar(10):
        sleep(0.2) #whatever task you need to do
    

提交回复
热议问题