multiple prints on the same line in Python

前端 未结 17 2270
独厮守ぢ
独厮守ぢ 2020-11-22 06:06

I want to run a script, which basically shows an output like this:

Installing XXX...               [DONE]

Currently, I print Installi

17条回答
  •  孤独总比滥情好
    2020-11-22 06:22

    I found this solution, and it's working on Python 2.7

    # Working on Python 2.7 Linux
    
    import time
    import sys
    
    
    def backspace(n):
        print('\r', end='')                     # use '\r' to go back
    
    
    for i in range(101):                        # for 0 to 100
        s = str(i) + '%'                        # string for output
        sys.stdout.write(string)
        backspace(len(s))                       # back for n chars
        sys.stdout.flush()
        time.sleep(0.2)                         # sleep for 200ms
    

提交回复
热议问题