Delete last line of input in Python

I have the following code:

num = int(raw_input("input number: "))
print "\b" * 20

The console output looks like

input number: 10

I want to delete text input number: 10after user click ENTER. The backspace key can \bnot do this.

+5
source share
4 answers

This will work on most unix and windows terminals ... it uses a very simple ANSI output.

num = int(raw_input("input number: "))
print "\033[A                             \033[A"    # ansi escape arrow up then overwrite the line

Please note that on Windows you may need to enable ANSI support using the following http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html

"\ 033 [A" is interpreted by the terminal as moving the cursor up one line.

+6
source

"" " " .., . , , , . . , Python curses " ".

, - - Windows. , Windows, .

+1
import sys

print "Welcome to a humble little screen control demo program"
print ""

# Clear the screen
#screen_code = "\033[2J";
#sys.stdout.write( screen_code )

# Go up to the previous line and then
# clear to the end of line
screen_code = "\033[1A[\033[2K"
sys.stdout.write( screen_code )
a = raw_input( "What a: " )
a = a.strip()
sys.stdout.write( screen_code )
b = raw_input( "What b: " )
b = b.strip()
print "a=[" , a , "]"
print "b=[" , b , "]"
0

os

import os
os.system('clear')

"cls" "clear" - , (, DOS ).

For IDLE: the best you can do is scroll the screen down a lot of lines, for example:

print "\n" * 100
-2
source

All Articles