clear shell screen

Gabriel Genellina gagsl-py2 at
Tue Oct 30 00:20:25 CET 2007

En Mon, 29 Oct 2007 17:31:52 -0300, Shawn Minisall  
<trekker182 at> escribió:

> works fine within the command line but then when I import os in
> python and then try os.system("cls"), i get that very fast
> opening/closing window and 0 inside the shell.

Try the other method menctioned in the Knowledge Base article:

How To Performing Clear Screen (CLS) in a Console Application


Some non-Microsoft versions of C++ provide a clrscr function
for clearing the screen in a DOS application. However, there
is no Win32 Application Programming Interface (API) or
C-Runtime function that will perform this function.
To accomplish this task for a Win32 console application, use
one of the following methods:
- os.system("cls")
- Write a function that will programmatically clear the screen

The following function is my (GG) Python translation of the
original C code as published in the Microsoft article, using
Mark Hammond's pywin32 extensions:

import win32api
import win32console

def cls():
     "Clear console screen"
     TopLeft = win32console.PyCOORDType(0,0)
     csb = win32console.GetStdHandle(win32api.STD_OUTPUT_HANDLE)
     csbi = csb.GetConsoleScreenBufferInfo()
     size = csbi['Size'].X * csbi['Size'].Y;
     csb.FillConsoleOutputCharacter(u' ', size, TopLeft)
     csb.FillConsoleOutputAttribute(csbi['Attributes'], size, TopLeft);


Gabriel Genellina

More information about the Python-list mailing list