Overwriting/clearing previous console line

前端 未结 2 1347
小鲜肉
小鲜肉 2021-01-18 01:40

My problem is, that I want to be able to overwrite/clear previous printed line in python console. This question has been asked many times (Python - Remove and Replace Printe

2条回答
  •  再見小時候
    2021-01-18 01:57

    You need to run your program form the command line, not from within IDLE.

    Then, this should work:

    import sys
    import time
    
    for i in range(10):
        sys.stdout.write("\r" + "Loading" + "." * i)
        time.sleep(1)
        sys.stdout.flush()
    print()
    

    The \r goes to beginning of the line. So you have to make sure the string you print is at least as long as the one before. Otherwise, you will see parts of the previous print.

提交回复
热议问题