简体   繁体   中英

Print to same line in console, over previous text, Python 3.6

I have tried all the scripts mentioned in other questions, but not working for me.

for x in range (0,10):
    print(x, end='\r', flush=True)
    print(x, sep='\r', end='', flush=True)
    print('\r', x)

Please help on this.

You probably want to overwrite the text, right?

You need to write backspaces to delete the last line and then print the next one. Check out this post for an example: How to create a spinning command line cursor?

Perhaps, you want a time.sleep(1) to have a better visualization, using stdout.write :

import sys
import time
for i in range(5):
    sys.stdout.write('\r' + str(i))
    time.sleep(1)

OUTPUT :

在此处输入图片说明

>>> for x in range(10):
...  print('\r', x, end='')
... 
 9>>> 

As you can see, all the other numbers got overwritten because to the carriage-return symbol \\r .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM