Clearing the screen

Steven D'Aprano steve at REMOVETHIScyber.com.au
Sun Feb 12 01:52:10 EST 2006


On Sat, 11 Feb 2006 18:14:02 -0200, Felipe Almeida Lessa wrote:

> Em Sáb, 2006-02-11 às 12:04 -0800, mwt escreveu:
>> I'm doing some python programming for a linux terminal (just learning).
>> When I want to completely redraw the screen, I've been using
>> os.system("clear")
>> This command works when using python in terminal mode, and in IDLE.
>> However, when running a little .py file containing that command, the
>> screen doesn't clear. 
>> What to do?
> 
> There's one escape sequence that does what you want.
> 
> I am *not* sure if this solution is the correct one, but:
> 
> $ clear | hd
> 00000000  1b 5b 48 1b 5b 32 4a                              |.[H.[2J|
> 00000007
> $ python
> Python 2.3.5 (#2, Nov 20 2005, 16:40:39)
> [GCC 4.0.3 20051111 (prerelease) (Debian 4.0.2-4)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> print
> chr(0x1b)+chr(0x5b)+chr(0x48)+chr(0x1b)+chr(0x5b)+chr(0x32)+chr(0x4a),

Or even easier:

print "\x1b[H\x1b[2J"


which may or may not work, depending on the terminal you are using.



-- 
Steven.




More information about the Python-list mailing list