remove last STDOUT line in Python remove last STDOUT line in Python python python

remove last STDOUT line in Python


You might be able to do what you want with VT100 control codes.

Something like this maybe:

CURSOR_UP_ONE = '\x1b[1A'ERASE_LINE = '\x1b[2K'print(CURSOR_UP_ONE + ERASE_LINE)


Give this a try:

CURSOR_UP = '\033[F'ERASE_LINE = '\033[K'print(CURSOR_UP + ERASE_LINE)


If what you're doing is reading a password, use getpass. There are a lot of subtle gotchas involved in reading a password safely; it's not code you want to write yourself.

If you are doing something less security-critical, disabling terminal echo can be done with the termios module. (I wish the example code there wasn't an incomplete, unsafe implementation of getpass, sigh. But it does show you how to turn off terminal echo.)

If you're on Windows, there is an equivalent but I don't know what it is, and it may not be exposed in the Python standard library.