8

I have the following code:

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

The console output looks like

input number: 10

I'd like to delete the text input number: 10 after the user presses ENTER. The backspace key \b can't do it.

1
  • which operating system do you work under? Commented May 31, 2012 at 8:11

4 Answers 4

10

This will work in most unix and windows terminals ... it uses very simple ANSI escape.

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 the ANSI support using the following http://www.windowsnetworking.com/kbase/windowstips/windows2000/usertips/miscellaneous/commandinterpreteransisupport.html

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

Sign up to request clarification or add additional context in comments.

10 Comments

I tried your code, but the row is still there (e.g. when I input number 10) it looks as follows: "input number: 0" "Done" and cursor is below it.
Are there enough spaces at the end of your string? Notice there is heaps of spaces at the end of my line.
So the 1 is erased but the 0 is not?
Yes, I understand it that you have overwritten the text, but I want to remove the whole text "input number: X" and move the cursor in front of "input" (which should already be deleted.)
The following string "\033[A" means put cursor one line up.
|
2
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 , "]"

Comments

1

There are control sequences for 'word back' and 'line back' and the like that move the cursor. So, you could try moving the curser back to the start of the text you want to delete, and overriding it with spaces. But this gets complicated very quickly. Thankfully, Python has the standard curses module for "advanced terminal handling".

The only issue with this is that it isn't cross-platform at the moment - that module has never been ported to Windows. So, if you need to support Windows, take a look at the Console module.

2 Comments

Would you know which functions from curses module would be helpful?
@xralf you will want to create window object by calling curses.initscr(), and get do your I/O using the window methods rather than print and raw_input. Then when you want to delete an entire line, you need to ensure that the cursor is somewhere on that line, and call .deleteln() on the window.
-3

You can use os module

import os
os.system('clear')

The "cls" and "clear" are commands which will clear a terminal (ie a DOS prompt, or terminal window).

For IDLE:The best you could do is to scroll the screen down lots of lines, eg:

print "\n" * 100

3 Comments

I know this command, but I'd like to remove only the text "input number: X" because the text above should not be removed.
Why do need to remove that...whats your exact requirement?
I'm reading interactively in a loop and don't want to have much text on the terminal. Explaining the whole context would be for long and off topic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.