How to overwrite the previous print to stdout in python?

后端 未结 16 1582
别那么骄傲
别那么骄傲 2020-11-22 09:46

If I had the following code:

for x in range(10):
     print x

I would get the output of

1
2
etc..

What I

16条回答
  •  失恋的感觉
    2020-11-22 10:24

    for x in range(10):
        time.sleep(0.5) # shows how its working
        print("\r {}".format(x), end="")
    

    time.sleep(0.5) is to show how previous output is erased and new output is printed "\r" when its at the start of print message , it gonna erase previous output before new output.

提交回复
热议问题