Clearing a DOS terminal in a script

Larry Bates larry.bates at websafe.com
Sun Dec 16 18:51:38 EST 2007


Stephen_B wrote:
> On Dec 13, 11:21 am, "Chris Mellon" <arka... at gmail.com> wrote:
>> It opens "clear" with it's own virtual terminal and clears that
>> instead.
> 
> Even when I launch the script from a cmd shell with "python
> myscript.py"?
> 
>> There's an ANSI control code you can use to reset the screen, try printing that.
> 
> I googled "<Esc>[2J" as an ascii sequence that it is supposed to clear
> the screen. How do I send that?
> 
> Stephen
Normally you would do:

import sys
sys.stdout.write(chr(27)+'[2J')
sys.stdout.flush()

Unfortunately that doesn't clear the screen because the ANSI module isn't loaded 
by default.  Use os.system('cls') as mentioned instead.

-Larry



More information about the Python-list mailing list