Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove last STDOUT line in Python

I am trying to figure out how to suppress the display of user input on stdout.

raw_input() followed by any print statement preserves what the user typed in. getpass() does not show what the user typed, but it does preserve the "Password:" prompt.

To fix this, I would like to only remove the last line (which would remove the newline from the end of the line as well).

like image 306
javanix Avatar asked Sep 25 '12 15:09

javanix


People also ask

How do you clear the previous print in Python?

system('cls') to clear the screen. The output window will print the given text first, then the program will sleep for 4 seconds and then screen will be cleared and program execution will be stopped.

How do you overwrite output in Python?

Approach. By default, Python's print statement ends each string that is passed into the function with a newline character, \n . This behavior can be overridden with the function's end parameter, which is the core of this method. Rather than ending the output with a newline, we use a carriage return.


2 Answers

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) 
like image 62
Some programmer dude Avatar answered Sep 21 '22 23:09

Some programmer dude


Give this a try:

CURSOR_UP = '\033[F' ERASE_LINE = '\033[K' print(CURSOR_UP + ERASE_LINE) 
like image 21
Ohad Cohen Avatar answered Sep 21 '22 23:09

Ohad Cohen