How to Clear Python Shell (IDLE)?


1 min read

Were there times when Python idle prints pages of traceback, or you've fiddled around long enough now you're at the bottom of the screen? All you need is a clear screen to start from scratch.

Unlike bash, the Python shell doesn't have the magic clear to make everything vanish. But, worry not, there are other ways to do it.

Using os module

import os
os.system("clear") # Linux/Mac
os.system("CLS")  #  For windows

The above snippet will clear the screen and add a 0 at the top. Why is that? It is the return value of the os.system method.

If you are as picky as me, you can easily work around it by assigning the return value to a variable.

import os
x = os.system("clear") # Linux/Mac
x = os.system("CLS")  #  For windows

Voila! Just clear the screen. No zeros.

Keyboard Shortcut

But is there a simpler way, after all, who wants to write such a long stream of strings to clear the screen

Command + L
Ctrl + L

Now this made me think if I can hook a script that adds a clear command to Python IDLE which I will be working on next. Subscribe to stay tuned.