How to overwrite the previous print to stdout in python?

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

    I had the same question before visiting this thread. For me the sys.stdout.write worked only if I properly flush the buffer i.e.

    for x in range(10):
        sys.stdout.write('\r'+str(x))
        sys.stdout.flush()
    

    Without flushing, the result is printed only at the end out the script

提交回复
热议问题