How to edit a string that was printed in stdout?

How to edit the line I just printed? For example, for a countdown counter (first prints 30, then changes it to 29, etc.)

Thank.

+5
source share
5 answers

Print a carriage return \rand it will return the cursor to the beginning of the line. Make sure that you do not print a new line \nat the end, because you cannot return lines. This means you need to do something like:

import time
import sys
sys.stdout.write('29 seconds remaining')
time.sleep(1)
sys.stdout.write('\r28 seconds remaining')

(Unlike use print, which adds a new line at the end of what it writes to stdout.)

+6
source

Unix/Linux, "" . , .. python: http://docs.python.org/library/curses.html

+1

xterm, , , . , , ( ), , . , , .

0

, . . , bradley.ayers, - .

0

You can use the readline module , which can also provide customized completion and command history .

0
source

All Articles