How to overwrite the previous print to stdout in python?

后端 未结 16 1419
别那么骄傲
别那么骄傲 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:27

    Better to overwrite the whole line otherwise the new line will mix with the old ones if the new line is shorter.

    import time, os
    for s in ['overwrite!', 'the!', 'whole!', 'line!']:
        print(s.ljust(os.get_terminal_size().columns - 1), end="\r")
        time.sleep(1)
    

    Had to use columns - 1 on Windows.

提交回复
热议问题