views:

6361

answers:

7

Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir() stuff, help() stuff, etc.

Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same command several times. I'm wondering if, and how, to clear the Python interpreter console.

I've heard about doing a system call and either calling cls on Windows or clear on Linux, but I was hoping there was something I could command the interpreter itself to do.

Note: I'm running on Windows, so Ctrl+L doesn't work.

+9  A: 

Well, here's a quick hack:

>>> clear = "\n" * 100
>>> print clear
>>> ...do some other stuff...
>>> print clear

Or to save some typing, put this file in your python search path:

# wiper.py
class Wipe(object):
    def __repr__(self):
        return '\n'*1000

wipe = Wipe()

Then you can do this from the interpreter all you like :)

>>> from wiper import wipe
>>> wipe
>>> wipe
>>> wipe
Triptych
Haha, that's pretty funny. Not exactly what I was looking for, but nice try.
Soviut
+1 for "\n" * 100. really easy to type.
Nick Stinemates
+18  A: 

As you mentioned, you can do a system call:

>>> clear = lambda: os.system('cls')
>>> clear()

I am not sure of any other way in Windows.

Ryan Duffield
This answer is closest to the 'spirit' of what I was asking for, thanks.
Soviut
+1, nice answer. Is there a way to consume the return value so that it doesn't show? I get a '0' after running clear.
technomalogical
Define it in a regular function instead of lambda should not show '0' as the return value will be None.
Akbar ibrahim
You could get fancy and use the subprocess module (Popen?), and redirect stdout to subprocess.PIPE if you wanted to totally suppress the output, I think.
Ryan Duffield
What's wrong with using `def`? Why use a `lambda` when a `def` is clearer?
S.Lott
+2  A: 

Use idle. It has many handy features. F6, for example, resets the console. Closing and opening the console are good ways to clear it.

S.Lott
how do you do that on idle? Just close and reopen?
Andrea Ambu
A: 

EDIT: I've just read "windows", this is for linux users, sorry.


In bash:

#!/bin/bash

while [ "0" == "0" ]; do
    clear
    $@
    while [ "$input" == "" ]; do
        read -p "Do you want to quit? (y/n): " -n 1 -e input
        if [ "$input" == "y" ]; then
            exit 1
        elif [ "$input" == "n" ]; then
            echo "Ok, keep working ;)"
        fi
    done
    input=""
done

Save it as "whatyouwant.sh", chmod +x it then run:

./whatyouwant.sh python

or something other than python (idle, whatever). This will ask you if you actually want to exit, if not it rerun python (or the command you gave as parameter).

This will clear all, the screen and all the variables/object/anything you created/imported in python.

In python just type exit() when you want to exit.

Andrea Ambu
+12  A: 

here something handy that is a little more cross-platform

import os

def cls():
    os.system(['clear','cls'][os.name == 'nt'])

# now, to clear the screen
cls()
popcnt
I love it when someone uses true or false as indexes on an array.
Manuel Ferreria
popcnt: you are obsolete! Use the ternary operator. :-)
kaizer.se
+2  A: 

Wiper is cool, good thing about it is I don't have to type '()' around it. Here is slight variation to it

# wiper.py
import os
class Wipe(object):
    def __repr__(self):
        os.system('cls')
        return ''

The usage will be similar to earlier.

Amol
I'd name it `class cls`.
martineau
+1  A: 

Although this is an older question, I thought I'd contribute something summing up what I think were the best of the other answers and add a wrinkle of my own by suggesting that you put these command(s) into a file and set your PYTHONSTARTUP environment variable to point to it. Since I'm on Windows at the moment, it's slightly biased that way, but could easily be slanted some other direction.

Anyway, here's my take on the code to put (or add to your existing) Python startup script:

# ==== pythonstartup.py ====

# add something to clear the screen
class cls(object):
    def __repr__(self):
        import os
        os.system('cls' if os.name == 'nt' else 'clear')
        return ''

cls = cls()

# ==== end pythonstartup.py ====

BTW, you can also use @Triptych's __repr__ trick to change exit() into just exit:

class exit(object):
    exit = exit # original object
    def __repr__(self):
        self.exit() # call original
        return ''

exit = exit()
martineau