tags:

views:

1692

answers:

7
A: 

Try this: os.system("cls")

Nick D
This works for me, but leaves a '0' at the top of the screen. To completely clear the screen, I need to assign the successful return code from `os.system` to a variable: `_ = os.system("cls")`
Paul McGuire
@Paul McGuire, good idea ;-) Now I see this question is a duplicate, S.Lott posted a link to a question with very good answers.
Nick D
+5  A: 

os.system('clear') works on linux. If you are running windows try os.system('CLS') instead.

You need to import os first like this:

import os
Nadia Alramli
Doesn't seem to work. Maybe I have to import something?
devoured elysium
You need to import os
Nadia Alramli
The os.system('CLS') works in Windows. (Python 2.6). The resulting screen is cleared, only showing a "0", which I assume is the return value for CLS, at the top. The next line has the >>> prompt.
mjv
Well, it doesn't clear the python shell. But I can see the 0 print.
devoured elysium
A: 

The way to execute commands in Python 2.4+ is to use the subprocess module. You can use it in the same way that you use os.system.

import subprocess
subprocess.call("clear") # linux/mac
subprocess.call("cls", shell=True) # windows

If you're executing this in the python console, you'll need to do something to hide the return value (for either os.system or subprocess.call), like assigning it to a variable:

cls = subprocess.call("cls", shell=True)
Rudd Zwolinski
Traceback (most recent call last): File "<pyshell#1>", line 1, in <module> subprocess.call("cls") File "C:\Python26\lib\subprocess.py", line 444, in call return Popen(*popenargs, **kwargs).wait() File "C:\Python26\lib\subprocess.py", line 595, in __init__ errread, errwrite) File "C:\Python26\lib\subprocess.py", line 804, in _execute_child startupinfo)WindowsError: [Error 2] System cannot find the specified file
devoured elysium
Hmm.. That's what I get for not using Windows and thus not being able to test it. Try subprocess.call("cls", shell=True)
Rudd Zwolinski
+2  A: 

The "cls" and "clear" are commands which will clear a terminal (ie a DOS prompt, or terminal window). From your screenshot, you are using the shell within IDLE, which won't be affected by such things. Unfortunately, I don't think there is a way to clear the screen in IDLE. The best you could do is to scroll the screen down lots of lines, eg:

print "\n" * 100

Though you could put this in a function:

def cls(): print "\n" * 100

And then call it when needed as cls()

Brian
+1  A: 

Your screenshot shows you're using IDLE. cls/clear won't work for you.

orip
+2  A: 

There does not appear to be a way to clear the IDLE 'shell' buffer.

A: 

As mark.ribau said, it seems that there is no way to clear the Text widget in idle. One should edit the EditorWindow.py module and add a method and a menu item in the EditorWindow class that does something like:

self.text.tag_remove("sel", "1.0", "end")
self.text.delete("1.0", "end")

and perhaps some more tag management of which I'm unaware of.

ΤΖΩΤΖΙΟΥ