How to clear the interpreter console?












259















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.










share|improve this question

























  • IPython solution here: stackoverflow.com/questions/6892191/…

    – onewhaleid
    Apr 26 '17 at 0:28











  • I'm surprised no one mentioned it. But when you are using Ipython shell you can do Ctrl + L even in windows

    – user93
    Feb 25 '18 at 3:18
















259















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.










share|improve this question

























  • IPython solution here: stackoverflow.com/questions/6892191/…

    – onewhaleid
    Apr 26 '17 at 0:28











  • I'm surprised no one mentioned it. But when you are using Ipython shell you can do Ctrl + L even in windows

    – user93
    Feb 25 '18 at 3:18














259












259








259


98






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.










share|improve this question
















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.







windows console clear python






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Feb 2 '17 at 12:34









martineau

67.1k1089180




67.1k1089180










asked Feb 5 '09 at 21:19









SoviutSoviut

57.5k33144208




57.5k33144208













  • IPython solution here: stackoverflow.com/questions/6892191/…

    – onewhaleid
    Apr 26 '17 at 0:28











  • I'm surprised no one mentioned it. But when you are using Ipython shell you can do Ctrl + L even in windows

    – user93
    Feb 25 '18 at 3:18



















  • IPython solution here: stackoverflow.com/questions/6892191/…

    – onewhaleid
    Apr 26 '17 at 0:28











  • I'm surprised no one mentioned it. But when you are using Ipython shell you can do Ctrl + L even in windows

    – user93
    Feb 25 '18 at 3:18

















IPython solution here: stackoverflow.com/questions/6892191/…

– onewhaleid
Apr 26 '17 at 0:28





IPython solution here: stackoverflow.com/questions/6892191/…

– onewhaleid
Apr 26 '17 at 0:28













I'm surprised no one mentioned it. But when you are using Ipython shell you can do Ctrl + L even in windows

– user93
Feb 25 '18 at 3:18





I'm surprised no one mentioned it. But when you are using Ipython shell you can do Ctrl + L even in windows

– user93
Feb 25 '18 at 3:18












33 Answers
33






active

oldest

votes













1 2
next












347














As you mentioned, you can do a system call:



For Windows



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


For Linux the lambda becomes



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





share|improve this answer





















  • 1





    This answer is closest to the 'spirit' of what I was asking for, thanks.

    – Soviut
    Feb 6 '09 at 0:18






  • 13





    Define it in a regular function instead of lambda should not show '0' as the return value will be None.

    – Akbar ibrahim
    Feb 6 '09 at 21:22






  • 41





    What's wrong with using def? Why use a lambda when a def is clearer?

    – S.Lott
    Sep 16 '09 at 12:31






  • 11





    Don't forget to import os

    – Old McStopher
    May 25 '11 at 16:49






  • 12





    @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

    – enpenax
    Jun 12 '13 at 18:13



















165














here something handy that is a little more cross-platform



import os

def cls():
os.system('cls' if os.name=='nt' else 'clear')

# now, to clear the screen
cls()





share|improve this answer


























  • Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

    – Torxed
    Oct 30 '12 at 16:22











  • import os os.system('cls')

    – Do Nhu Vy
    Jul 10 '16 at 12:26



















78














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





share|improve this answer





















  • 10





    Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

    – Soviut
    Feb 5 '09 at 21:23






  • 8





    +1 for "n" * 100. really easy to type.

    – Nick Stinemates
    Feb 5 '09 at 21:49






  • 2





    @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

    – pythonbeginer
    Sep 20 '11 at 21:02






  • 6





    Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

    – jsbueno
    Jun 2 '16 at 5:27






  • 4





    @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

    – spectras
    Sep 1 '18 at 11:56



















28














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.



Here's some articles I found that describe how to set environment variables on Windows:

    When to use sys.path.append and when modifying %PYTHONPATH% is enough

    How To Manage Environment Variables in Windows XP

    Configuring System and User Environment Variables

    How to Use Global System Environment Variables in Windows



BTW, don't put quotes around the path to the file even if it has spaces in it.



Anyway, here's my take on the code to put in (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 (and ditto for its alias quit):



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

quit = exit = exit()


Lastly, here's something else that changes the primary interpreter prompt from >>> to cwd+>>>:



class Prompt:
def __str__(self):
import os
return '%s >>> ' % os.getcwd()

import sys
sys.ps1 = Prompt()
del sys
del Prompt





share|improve this answer


























  • This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

    – Triptych
    Nov 4 '12 at 1:00











  • @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

    – martineau
    Nov 29 '12 at 19:55











  • interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

    – Triptych
    Nov 29 '12 at 21:49













  • @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

    – martineau
    Nov 29 '12 at 23:31











  • I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

    – Triptych
    Nov 30 '12 at 0:02



















20














You have number of ways doing it on Windows:



1. Using Keyboard shortcut:



Press CTRL + L


2. Using system invoke method:



import os
cls = lambda: os.system('cls')
cls()


3. Using new line print 100 times:



cls = lambda: print('n'*100)
cls()





share|improve this answer



















  • 1





    All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

    – Luke
    Nov 16 '16 at 9:31





















17














Quickest and easiest way without a doubt is Ctrl+L.



This is the same for OS X on the terminal.






share|improve this answer



















  • 3





    I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

    – Chris22
    Jan 11 '16 at 18:27






  • 3





    Yeah that is the easiest. I am surprised no one mentioned it earlier.

    – Abhishek Bhatia
    Feb 10 '16 at 1:49








  • 2





    On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

    – Andrew Franklin
    Jun 1 '16 at 18:30






  • 2





    Work beautifully for me in Ubuntu Desktop 16

    – Nam G VU
    Jul 21 '16 at 6:22



















12














my way of doing this is to write a function like so:



import os
import subprocess

def clear():
if os.name in ('nt','dos'):
subprocess.call("cls")
elif os.name in ('linux','osx','posix'):
subprocess.call("clear")
else:
print("n") * 120


then call clear() to clear the screen.
this works on windows, osx, linux, bsd... all OSes.






share|improve this answer


























  • You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

    – rsanden
    Jul 24 '15 at 3:17











  • @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

    – MartinUbuntu
    Jul 24 '15 at 16:26











  • I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

    – rsanden
    Jul 24 '15 at 16:46











  • @rsanden added posix.

    – MartinUbuntu
    Aug 16 '15 at 10:41





















8














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 Cls(object):
def __repr__(self):
        os.system('cls')
return ''


The usage is quite simple:



>>> cls = Cls()
>>> cls # this will clear console.





share|improve this answer





















  • 2





    I'd name it class cls.

    – martineau
    Oct 12 '10 at 17:25






  • 1





    I'd name the instance of the class Cls to be cls. cls = Cls()

    – Amol
    Dec 27 '11 at 5:47













  • Except that pollutes the initial namespace with two things instead of one...twice as much.

    – martineau
    Dec 27 '11 at 19:23













  • +1 An original way to make commands in Python.

    – Alba Mendez
    Jul 10 '12 at 12:14











  • @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

    – Alba Mendez
    Jul 17 '12 at 16:29



















5














Here's the definitive solution that merges all other answers. Features:




  1. You can copy-paste the code into your shell or script.


  2. You can use it as you like:



    >>> clear()
    >>> -clear
    >>> clear # <- but this will only work on a shell



  3. You can import it as a module:



    >>> from clear import clear
    >>> -clear



  4. You can call it as a script:



    $ python clear.py


  5. It is truly multiplatform; if it can't recognize your system

    (ce, nt, dos or posix) it will fall back to printing blank lines.





You can download the [full] file here: https://gist.github.com/3130325

Or if you are just looking for the code:



class clear:
def __call__(self):
import os
if os.name==('ce','nt','dos'): os.system('cls')
elif os.name=='posix': os.system('clear')
else: print('n'*120)
def __neg__(self): self()
def __repr__(self):
self();return ''

clear=clear()





share|improve this answer

































    4














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






    share|improve this answer


























    • how do you do that on idle? Just close and reopen?

      – Andrea Ambu
      Feb 5 '09 at 23:08






    • 5





      No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

      – ridgerunner
      May 1 '11 at 14:58



















    4














    I use iTerm and the native terminal app for Mac OS.



    I just press ⌘ + k






    share|improve this answer

































      4














      Here's a cross platform (Windows / Linux / Mac / Probably others that you can add in the if check) version snippet I made combining information found in this question:



      import os
      clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
      clear()


      Same idea but with a spoon of syntactic sugar:



      import subprocess   
      clear = lambda: subprocess.call('cls||clear', shell=True)
      clear()





      share|improve this answer

































        3














        I'm not sure if Windows' "shell" supports this, but on Linux:



        print "33[2J"



        https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes



        In my opinion calling cls with os is a bad idea generally. Imagine if I manage to change the cls or clear command on your system, and you run your script as admin or root.






        share|improve this answer

































          2














          I'm using MINGW/BASH on Windows XP, SP3.



          (stick this in .pythonstartup)

          # My ctrl-l already kind of worked, but this might help someone else

          # leaves prompt at bottom of the window though...

          import readline

          readline.parse_and_bind('C-l: clear-screen')



          # This works in BASH because I have it in .inputrc as well, but for some

          # reason it gets dropped when I go into Python

          readline.parse_and_bind('C-y: kill-whole-line')





          I couldn't stand typing 'exit()' anymore and was delighted with martineau's/Triptych's tricks:



          I slightly doctored it though (stuck it in .pythonstartup)



          class exxxit():
          """Shortcut for exit() function, use 'x' now"""
          quit_now = exit # original object
          def __repr__(self):
          self.quit_now() # call original
          x = exxxit()




          Py2.7.1>help(x)
          Help on instance of exxxit in module __main__:

          class exxxit
          | Shortcut for exit() function, use 'x' now
          |
          | Methods defined here:
          |
          | __repr__(self)
          |
          | ----------------------------------------------------------------------
          | Data and other attributes defined here:
          |
          | quit_now = Use exit() or Ctrl-Z plus Return to exit





          share|improve this answer

































            2














            Here are two nice ways of doing that:



            1.



            import os

            # Clear Windows command prompt.
            if (os.name in ('ce', 'nt', 'dos')):
            os.system('cls')

            # Clear the Linux terminal.
            elif ('posix' in os.name):
            os.system('clear')


            2.



            import os

            def clear():
            if os.name == 'posix':
            os.system('clear')

            elif os.name in ('ce', 'nt', 'dos'):
            os.system('cls')


            clear()





            share|improve this answer


























            • If os.name is none of these, why not fall back to printing empty lines?

              – Alba Mendez
              Jul 10 '12 at 12:16











            • If it's Jython then I want you to know that os.name = 'java'

              – iChux
              Nov 29 '13 at 10:00











            • Looks good, does this mean that the program can become cross platform?

              – Luke
              Nov 16 '16 at 9:33



















            1














            How about this for a clear



            - os.system('cls')


            That is about as short as could be!






            share|improve this answer


























            • yes, but not multiplatform and you have to retype it every time you want to clear the screen.

              – Alba Mendez
              Jul 10 '12 at 12:22



















            1














            The OS command clear in Linux and cls in Windows outputs a "magic string" which you can just print. To get the string, execute the command with popen and save it in a variable for later use:



            from os import popen
            with popen('clear') as f:
            clear = f.read()

            print clear


            On my machine the string is 'x1b[Hx1b[2J'.






            share|improve this answer



















            • 1





              1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

              – Alba Mendez
              Jul 10 '12 at 12:20











            • Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

              – DenMark
              Jul 25 '17 at 7:04



















            1














            I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:



            Open shell / Create new document / Create function as follows:



            def clear():
            print('n' * 50)


            Save it inside the lib folder in you python directory (mine is C:Python33Lib)
            Next time you nedd to clear your console just call the function with:



            clear()


            that's it.
            PS: you can name you function anyway you want. Iv' seen people using "wiper" "wipe" and variations.






            share|improve this answer

































              1














              I found the simplest way is just to close the window and run a module/script to reopen the shell.






              share|improve this answer































                1














                just use this..



                print 'n'*1000






                share|improve this answer































                  1














                  I might be late to the part but here is a very easy way to do it



                  Type:



                  def cls():
                  os.system("cls")


                  So what ever you want to clear the screen just type in your code



                  cls()


                  Best way possible! (Credit : https://www.youtube.com/watch?annotation_id=annotation_3770292585&feature=iv&src_vid=bguKhMnvmb8&v=LtGEp9c6Z-U)






                  share|improve this answer































                    1














                    This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return >>> to the top left corner.



                    print("33[H33[J")





                    share|improve this answer



















                    • 2





                      It does not work with Windows Python 3.7 version.

                      – Gunny KC
                      Jul 7 '18 at 5:00



















                    0














                    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.






                    share|improve this answer































                      0














                      This should be cross platform, and also uses the preferred subprocess.call instead of os.system as per the os.system docs. Should work in Python >= 2.4.



                      import subprocess
                      import os

                      if os.name == 'nt':
                      def clearscreen():
                      subprocess.call("cls", shell=True)
                      return
                      else:
                      def clearscreen():
                      subprocess.call("clear", shell=True)
                      return





                      share|improve this answer































                        0














                        OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". Hope this helps someone out there!






                        share|improve this answer































                          0














                          >>> ' '*80*25


                          UPDATE: 80x25 is unlikely to be the size of console windows, so to get the real console dimensions, use functions from pager module. Python doesn't provide anything similar from core distribution.



                          >>> from pager import getheight
                          >>> 'n' * getheight()





                          share|improve this answer


























                          • That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                            – Alba Mendez
                            Jul 10 '12 at 12:24











                          • Shrt ansrs rck!

                            – anatoly techtonik
                            Jul 16 '12 at 10:54













                          • It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                            – aramis
                            Jul 17 '12 at 22:34











                          • Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                            – anatoly techtonik
                            Jul 18 '12 at 8:43



















                          0














                          Magic strings are mentioned above - I believe they come from the terminfo database:



                          http://www.google.com/?q=x#q=terminfo



                          http://www.google.com/?q=x#q=tput+command+in+unix



                          $ tput clear| od -t x1z
                          0000000 1b 5b 48 1b 5b 32 4a >.[H.[2J<
                          0000007






                          share|improve this answer































                            0














                            I am using Spyder (Python 2.7) and to clean the interpreter console I use either



                            %clear



                            that forces the command line to go to the top and I will not see the previous old commands.



                            or I click "option" on the Console environment and select "Restart kernel" that removes everything.






                            share|improve this answer































                              0














                              Just enter



                              import os
                              os.system('cls') # Windows
                              os.system('clear') # Linux, Unix, Mac OS X





                              share|improve this answer































                                0














                                The easiest way
                                `>>> import os






                                clear = lambda: os.system('clear')
                                clear()`









                                share|improve this answer



























                                  1 2
                                  next


                                  protected by vsoftco Oct 18 '16 at 2:17



                                  Thank you for your interest in this question.
                                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                  Would you like to answer one of these unanswered questions instead?














                                  33 Answers
                                  33






                                  active

                                  oldest

                                  votes








                                  33 Answers
                                  33






                                  active

                                  oldest

                                  votes









                                  active

                                  oldest

                                  votes






                                  active

                                  oldest

                                  votes








                                  1 2
                                  next










                                  347














                                  As you mentioned, you can do a system call:



                                  For Windows



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


                                  For Linux the lambda becomes



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





                                  share|improve this answer





















                                  • 1





                                    This answer is closest to the 'spirit' of what I was asking for, thanks.

                                    – Soviut
                                    Feb 6 '09 at 0:18






                                  • 13





                                    Define it in a regular function instead of lambda should not show '0' as the return value will be None.

                                    – Akbar ibrahim
                                    Feb 6 '09 at 21:22






                                  • 41





                                    What's wrong with using def? Why use a lambda when a def is clearer?

                                    – S.Lott
                                    Sep 16 '09 at 12:31






                                  • 11





                                    Don't forget to import os

                                    – Old McStopher
                                    May 25 '11 at 16:49






                                  • 12





                                    @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

                                    – enpenax
                                    Jun 12 '13 at 18:13
















                                  347














                                  As you mentioned, you can do a system call:



                                  For Windows



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


                                  For Linux the lambda becomes



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





                                  share|improve this answer





















                                  • 1





                                    This answer is closest to the 'spirit' of what I was asking for, thanks.

                                    – Soviut
                                    Feb 6 '09 at 0:18






                                  • 13





                                    Define it in a regular function instead of lambda should not show '0' as the return value will be None.

                                    – Akbar ibrahim
                                    Feb 6 '09 at 21:22






                                  • 41





                                    What's wrong with using def? Why use a lambda when a def is clearer?

                                    – S.Lott
                                    Sep 16 '09 at 12:31






                                  • 11





                                    Don't forget to import os

                                    – Old McStopher
                                    May 25 '11 at 16:49






                                  • 12





                                    @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

                                    – enpenax
                                    Jun 12 '13 at 18:13














                                  347












                                  347








                                  347







                                  As you mentioned, you can do a system call:



                                  For Windows



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


                                  For Linux the lambda becomes



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





                                  share|improve this answer















                                  As you mentioned, you can do a system call:



                                  For Windows



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


                                  For Linux the lambda becomes



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






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Aug 6 '18 at 17:02









                                  angelo.mastro

                                  13416




                                  13416










                                  answered Feb 5 '09 at 21:25









                                  Ryan DuffieldRyan Duffield

                                  13.7k53438




                                  13.7k53438








                                  • 1





                                    This answer is closest to the 'spirit' of what I was asking for, thanks.

                                    – Soviut
                                    Feb 6 '09 at 0:18






                                  • 13





                                    Define it in a regular function instead of lambda should not show '0' as the return value will be None.

                                    – Akbar ibrahim
                                    Feb 6 '09 at 21:22






                                  • 41





                                    What's wrong with using def? Why use a lambda when a def is clearer?

                                    – S.Lott
                                    Sep 16 '09 at 12:31






                                  • 11





                                    Don't forget to import os

                                    – Old McStopher
                                    May 25 '11 at 16:49






                                  • 12





                                    @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

                                    – enpenax
                                    Jun 12 '13 at 18:13














                                  • 1





                                    This answer is closest to the 'spirit' of what I was asking for, thanks.

                                    – Soviut
                                    Feb 6 '09 at 0:18






                                  • 13





                                    Define it in a regular function instead of lambda should not show '0' as the return value will be None.

                                    – Akbar ibrahim
                                    Feb 6 '09 at 21:22






                                  • 41





                                    What's wrong with using def? Why use a lambda when a def is clearer?

                                    – S.Lott
                                    Sep 16 '09 at 12:31






                                  • 11





                                    Don't forget to import os

                                    – Old McStopher
                                    May 25 '11 at 16:49






                                  • 12





                                    @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

                                    – enpenax
                                    Jun 12 '13 at 18:13








                                  1




                                  1





                                  This answer is closest to the 'spirit' of what I was asking for, thanks.

                                  – Soviut
                                  Feb 6 '09 at 0:18





                                  This answer is closest to the 'spirit' of what I was asking for, thanks.

                                  – Soviut
                                  Feb 6 '09 at 0:18




                                  13




                                  13





                                  Define it in a regular function instead of lambda should not show '0' as the return value will be None.

                                  – Akbar ibrahim
                                  Feb 6 '09 at 21:22





                                  Define it in a regular function instead of lambda should not show '0' as the return value will be None.

                                  – Akbar ibrahim
                                  Feb 6 '09 at 21:22




                                  41




                                  41





                                  What's wrong with using def? Why use a lambda when a def is clearer?

                                  – S.Lott
                                  Sep 16 '09 at 12:31





                                  What's wrong with using def? Why use a lambda when a def is clearer?

                                  – S.Lott
                                  Sep 16 '09 at 12:31




                                  11




                                  11





                                  Don't forget to import os

                                  – Old McStopher
                                  May 25 '11 at 16:49





                                  Don't forget to import os

                                  – Old McStopher
                                  May 25 '11 at 16:49




                                  12




                                  12





                                  @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

                                  – enpenax
                                  Jun 12 '13 at 18:13





                                  @Akbaribrahim as None will not be printed try this: clear = lambda: os.system('cls') or None

                                  – enpenax
                                  Jun 12 '13 at 18:13













                                  165














                                  here something handy that is a little more cross-platform



                                  import os

                                  def cls():
                                  os.system('cls' if os.name=='nt' else 'clear')

                                  # now, to clear the screen
                                  cls()





                                  share|improve this answer


























                                  • Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

                                    – Torxed
                                    Oct 30 '12 at 16:22











                                  • import os os.system('cls')

                                    – Do Nhu Vy
                                    Jul 10 '16 at 12:26
















                                  165














                                  here something handy that is a little more cross-platform



                                  import os

                                  def cls():
                                  os.system('cls' if os.name=='nt' else 'clear')

                                  # now, to clear the screen
                                  cls()





                                  share|improve this answer


























                                  • Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

                                    – Torxed
                                    Oct 30 '12 at 16:22











                                  • import os os.system('cls')

                                    – Do Nhu Vy
                                    Jul 10 '16 at 12:26














                                  165












                                  165








                                  165







                                  here something handy that is a little more cross-platform



                                  import os

                                  def cls():
                                  os.system('cls' if os.name=='nt' else 'clear')

                                  # now, to clear the screen
                                  cls()





                                  share|improve this answer















                                  here something handy that is a little more cross-platform



                                  import os

                                  def cls():
                                  os.system('cls' if os.name=='nt' else 'clear')

                                  # now, to clear the screen
                                  cls()






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Oct 21 '15 at 9:55









                                  rubenvb

                                  53.1k22138259




                                  53.1k22138259










                                  answered Mar 26 '09 at 2:42









                                  popcntpopcnt

                                  3,33311214




                                  3,33311214













                                  • Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

                                    – Torxed
                                    Oct 30 '12 at 16:22











                                  • import os os.system('cls')

                                    – Do Nhu Vy
                                    Jul 10 '16 at 12:26



















                                  • Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

                                    – Torxed
                                    Oct 30 '12 at 16:22











                                  • import os os.system('cls')

                                    – Do Nhu Vy
                                    Jul 10 '16 at 12:26

















                                  Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

                                  – Torxed
                                  Oct 30 '12 at 16:22





                                  Great way of going about things, Combine this with the lambda suggestion above to save a row of code, handy as hell! Thank you! :)

                                  – Torxed
                                  Oct 30 '12 at 16:22













                                  import os os.system('cls')

                                  – Do Nhu Vy
                                  Jul 10 '16 at 12:26





                                  import os os.system('cls')

                                  – Do Nhu Vy
                                  Jul 10 '16 at 12:26











                                  78














                                  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





                                  share|improve this answer





















                                  • 10





                                    Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

                                    – Soviut
                                    Feb 5 '09 at 21:23






                                  • 8





                                    +1 for "n" * 100. really easy to type.

                                    – Nick Stinemates
                                    Feb 5 '09 at 21:49






                                  • 2





                                    @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

                                    – pythonbeginer
                                    Sep 20 '11 at 21:02






                                  • 6





                                    Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

                                    – jsbueno
                                    Jun 2 '16 at 5:27






                                  • 4





                                    @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

                                    – spectras
                                    Sep 1 '18 at 11:56
















                                  78














                                  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





                                  share|improve this answer





















                                  • 10





                                    Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

                                    – Soviut
                                    Feb 5 '09 at 21:23






                                  • 8





                                    +1 for "n" * 100. really easy to type.

                                    – Nick Stinemates
                                    Feb 5 '09 at 21:49






                                  • 2





                                    @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

                                    – pythonbeginer
                                    Sep 20 '11 at 21:02






                                  • 6





                                    Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

                                    – jsbueno
                                    Jun 2 '16 at 5:27






                                  • 4





                                    @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

                                    – spectras
                                    Sep 1 '18 at 11:56














                                  78












                                  78








                                  78







                                  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





                                  share|improve this answer















                                  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






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Feb 5 '09 at 21:29

























                                  answered Feb 5 '09 at 21:22









                                  TriptychTriptych

                                  154k29135165




                                  154k29135165








                                  • 10





                                    Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

                                    – Soviut
                                    Feb 5 '09 at 21:23






                                  • 8





                                    +1 for "n" * 100. really easy to type.

                                    – Nick Stinemates
                                    Feb 5 '09 at 21:49






                                  • 2





                                    @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

                                    – pythonbeginer
                                    Sep 20 '11 at 21:02






                                  • 6





                                    Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

                                    – jsbueno
                                    Jun 2 '16 at 5:27






                                  • 4





                                    @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

                                    – spectras
                                    Sep 1 '18 at 11:56














                                  • 10





                                    Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

                                    – Soviut
                                    Feb 5 '09 at 21:23






                                  • 8





                                    +1 for "n" * 100. really easy to type.

                                    – Nick Stinemates
                                    Feb 5 '09 at 21:49






                                  • 2





                                    @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

                                    – pythonbeginer
                                    Sep 20 '11 at 21:02






                                  • 6





                                    Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

                                    – jsbueno
                                    Jun 2 '16 at 5:27






                                  • 4





                                    @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

                                    – spectras
                                    Sep 1 '18 at 11:56








                                  10




                                  10





                                  Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

                                  – Soviut
                                  Feb 5 '09 at 21:23





                                  Haha, that's pretty funny. Not exactly what I was looking for, but nice try.

                                  – Soviut
                                  Feb 5 '09 at 21:23




                                  8




                                  8





                                  +1 for "n" * 100. really easy to type.

                                  – Nick Stinemates
                                  Feb 5 '09 at 21:49





                                  +1 for "n" * 100. really easy to type.

                                  – Nick Stinemates
                                  Feb 5 '09 at 21:49




                                  2




                                  2





                                  @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

                                  – pythonbeginer
                                  Sep 20 '11 at 21:02





                                  @Triptych: c = "n" * 100 usefull, +1 for it. A small comment it clears and brings to the bottom of the shell, i prefer to start from the shell top.

                                  – pythonbeginer
                                  Sep 20 '11 at 21:02




                                  6




                                  6





                                  Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

                                  – jsbueno
                                  Jun 2 '16 at 5:27





                                  Despite people laughing, printing a lot of newlines is exactly what the external processes clear and cls do. This is the way to do it. (just a print, or a function with the print call, not assigining to a "clear" string, of course)

                                  – jsbueno
                                  Jun 2 '16 at 5:27




                                  4




                                  4





                                  @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

                                  – spectras
                                  Sep 1 '18 at 11:56





                                  @jsbueno no it's not. Well maybe on windows (though I doubt it, it has APIs to clear the console). On all other systems, clear outputs a directive that clears the screen. Without trashing the scrollback buffer. On my system, it outputs this control sequence: 33[3J33[H33[2J. That is: [erase scrollback] [reset cursor position] [erase screen]. The [erase scrollback] can be omitted using clear -x.

                                  – spectras
                                  Sep 1 '18 at 11:56











                                  28














                                  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.



                                  Here's some articles I found that describe how to set environment variables on Windows:

                                      When to use sys.path.append and when modifying %PYTHONPATH% is enough

                                      How To Manage Environment Variables in Windows XP

                                      Configuring System and User Environment Variables

                                      How to Use Global System Environment Variables in Windows



                                  BTW, don't put quotes around the path to the file even if it has spaces in it.



                                  Anyway, here's my take on the code to put in (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 (and ditto for its alias quit):



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

                                  quit = exit = exit()


                                  Lastly, here's something else that changes the primary interpreter prompt from >>> to cwd+>>>:



                                  class Prompt:
                                  def __str__(self):
                                  import os
                                  return '%s >>> ' % os.getcwd()

                                  import sys
                                  sys.ps1 = Prompt()
                                  del sys
                                  del Prompt





                                  share|improve this answer


























                                  • This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

                                    – Triptych
                                    Nov 4 '12 at 1:00











                                  • @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

                                    – martineau
                                    Nov 29 '12 at 19:55











                                  • interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

                                    – Triptych
                                    Nov 29 '12 at 21:49













                                  • @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

                                    – martineau
                                    Nov 29 '12 at 23:31











                                  • I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

                                    – Triptych
                                    Nov 30 '12 at 0:02
















                                  28














                                  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.



                                  Here's some articles I found that describe how to set environment variables on Windows:

                                      When to use sys.path.append and when modifying %PYTHONPATH% is enough

                                      How To Manage Environment Variables in Windows XP

                                      Configuring System and User Environment Variables

                                      How to Use Global System Environment Variables in Windows



                                  BTW, don't put quotes around the path to the file even if it has spaces in it.



                                  Anyway, here's my take on the code to put in (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 (and ditto for its alias quit):



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

                                  quit = exit = exit()


                                  Lastly, here's something else that changes the primary interpreter prompt from >>> to cwd+>>>:



                                  class Prompt:
                                  def __str__(self):
                                  import os
                                  return '%s >>> ' % os.getcwd()

                                  import sys
                                  sys.ps1 = Prompt()
                                  del sys
                                  del Prompt





                                  share|improve this answer


























                                  • This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

                                    – Triptych
                                    Nov 4 '12 at 1:00











                                  • @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

                                    – martineau
                                    Nov 29 '12 at 19:55











                                  • interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

                                    – Triptych
                                    Nov 29 '12 at 21:49













                                  • @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

                                    – martineau
                                    Nov 29 '12 at 23:31











                                  • I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

                                    – Triptych
                                    Nov 30 '12 at 0:02














                                  28












                                  28








                                  28







                                  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.



                                  Here's some articles I found that describe how to set environment variables on Windows:

                                      When to use sys.path.append and when modifying %PYTHONPATH% is enough

                                      How To Manage Environment Variables in Windows XP

                                      Configuring System and User Environment Variables

                                      How to Use Global System Environment Variables in Windows



                                  BTW, don't put quotes around the path to the file even if it has spaces in it.



                                  Anyway, here's my take on the code to put in (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 (and ditto for its alias quit):



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

                                  quit = exit = exit()


                                  Lastly, here's something else that changes the primary interpreter prompt from >>> to cwd+>>>:



                                  class Prompt:
                                  def __str__(self):
                                  import os
                                  return '%s >>> ' % os.getcwd()

                                  import sys
                                  sys.ps1 = Prompt()
                                  del sys
                                  del Prompt





                                  share|improve this answer















                                  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.



                                  Here's some articles I found that describe how to set environment variables on Windows:

                                      When to use sys.path.append and when modifying %PYTHONPATH% is enough

                                      How To Manage Environment Variables in Windows XP

                                      Configuring System and User Environment Variables

                                      How to Use Global System Environment Variables in Windows



                                  BTW, don't put quotes around the path to the file even if it has spaces in it.



                                  Anyway, here's my take on the code to put in (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 (and ditto for its alias quit):



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

                                  quit = exit = exit()


                                  Lastly, here's something else that changes the primary interpreter prompt from >>> to cwd+>>>:



                                  class Prompt:
                                  def __str__(self):
                                  import os
                                  return '%s >>> ' % os.getcwd()

                                  import sys
                                  sys.ps1 = Prompt()
                                  del sys
                                  del Prompt






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited May 23 '17 at 12:10









                                  Community

                                  11




                                  11










                                  answered Oct 12 '10 at 18:36









                                  martineaumartineau

                                  67.1k1089180




                                  67.1k1089180













                                  • This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

                                    – Triptych
                                    Nov 4 '12 at 1:00











                                  • @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

                                    – martineau
                                    Nov 29 '12 at 19:55











                                  • interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

                                    – Triptych
                                    Nov 29 '12 at 21:49













                                  • @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

                                    – martineau
                                    Nov 29 '12 at 23:31











                                  • I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

                                    – Triptych
                                    Nov 30 '12 at 0:02



















                                  • This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

                                    – Triptych
                                    Nov 4 '12 at 1:00











                                  • @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

                                    – martineau
                                    Nov 29 '12 at 19:55











                                  • interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

                                    – Triptych
                                    Nov 29 '12 at 21:49













                                  • @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

                                    – martineau
                                    Nov 29 '12 at 23:31











                                  • I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

                                    – Triptych
                                    Nov 30 '12 at 0:02

















                                  This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

                                  – Triptych
                                  Nov 4 '12 at 1:00





                                  This is probably the best answer - a combination of techniques from other answers. PYTHONSTARTUP + repr + os.system('cls'). Very nice.

                                  – Triptych
                                  Nov 4 '12 at 1:00













                                  @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

                                  – martineau
                                  Nov 29 '12 at 19:55





                                  @Triptych: One interesting side-effect of using __repr__ and/or __str__ this way is that if you type >>> vars() at the interpreter console, it will execute all the commands thusly defined. On my system, for example, it cleared the screen and then exited the console. Took me a while to figure out what the heck was going on....

                                  – martineau
                                  Nov 29 '12 at 19:55













                                  interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

                                  – Triptych
                                  Nov 29 '12 at 21:49







                                  interesting. I see this problem also applies to locals() and globals(). A simple decorator around these functions that deletes the name and reassigns it after function invocation is a possible solution...

                                  – Triptych
                                  Nov 29 '12 at 21:49















                                  @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

                                  – martineau
                                  Nov 29 '12 at 23:31





                                  @Triptych: The decorator idea doesn't seem to work, at least with my own attempts. Coming up with an viable alternative is proving to surprising difficult.

                                  – martineau
                                  Nov 29 '12 at 23:31













                                  I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

                                  – Triptych
                                  Nov 30 '12 at 0:02





                                  I have a candidate solution that simply munges the result of vars() globals() and locals() calls temporarily: gist.github.com/4172781

                                  – Triptych
                                  Nov 30 '12 at 0:02











                                  20














                                  You have number of ways doing it on Windows:



                                  1. Using Keyboard shortcut:



                                  Press CTRL + L


                                  2. Using system invoke method:



                                  import os
                                  cls = lambda: os.system('cls')
                                  cls()


                                  3. Using new line print 100 times:



                                  cls = lambda: print('n'*100)
                                  cls()





                                  share|improve this answer



















                                  • 1





                                    All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

                                    – Luke
                                    Nov 16 '16 at 9:31


















                                  20














                                  You have number of ways doing it on Windows:



                                  1. Using Keyboard shortcut:



                                  Press CTRL + L


                                  2. Using system invoke method:



                                  import os
                                  cls = lambda: os.system('cls')
                                  cls()


                                  3. Using new line print 100 times:



                                  cls = lambda: print('n'*100)
                                  cls()





                                  share|improve this answer



















                                  • 1





                                    All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

                                    – Luke
                                    Nov 16 '16 at 9:31
















                                  20












                                  20








                                  20







                                  You have number of ways doing it on Windows:



                                  1. Using Keyboard shortcut:



                                  Press CTRL + L


                                  2. Using system invoke method:



                                  import os
                                  cls = lambda: os.system('cls')
                                  cls()


                                  3. Using new line print 100 times:



                                  cls = lambda: print('n'*100)
                                  cls()





                                  share|improve this answer













                                  You have number of ways doing it on Windows:



                                  1. Using Keyboard shortcut:



                                  Press CTRL + L


                                  2. Using system invoke method:



                                  import os
                                  cls = lambda: os.system('cls')
                                  cls()


                                  3. Using new line print 100 times:



                                  cls = lambda: print('n'*100)
                                  cls()






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Jun 20 '16 at 14:46









                                  Vlad BezdenVlad Bezden

                                  29.1k10128114




                                  29.1k10128114








                                  • 1





                                    All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

                                    – Luke
                                    Nov 16 '16 at 9:31
















                                  • 1





                                    All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

                                    – Luke
                                    Nov 16 '16 at 9:31










                                  1




                                  1





                                  All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

                                  – Luke
                                  Nov 16 '16 at 9:31







                                  All these ways are pretty... naff. cls will only work on windows and make your program hard to be cross platform, printing 100 newlines is just... eww? And a keyboard shortcut is not used in the program.

                                  – Luke
                                  Nov 16 '16 at 9:31













                                  17














                                  Quickest and easiest way without a doubt is Ctrl+L.



                                  This is the same for OS X on the terminal.






                                  share|improve this answer



















                                  • 3





                                    I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

                                    – Chris22
                                    Jan 11 '16 at 18:27






                                  • 3





                                    Yeah that is the easiest. I am surprised no one mentioned it earlier.

                                    – Abhishek Bhatia
                                    Feb 10 '16 at 1:49








                                  • 2





                                    On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

                                    – Andrew Franklin
                                    Jun 1 '16 at 18:30






                                  • 2





                                    Work beautifully for me in Ubuntu Desktop 16

                                    – Nam G VU
                                    Jul 21 '16 at 6:22
















                                  17














                                  Quickest and easiest way without a doubt is Ctrl+L.



                                  This is the same for OS X on the terminal.






                                  share|improve this answer



















                                  • 3





                                    I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

                                    – Chris22
                                    Jan 11 '16 at 18:27






                                  • 3





                                    Yeah that is the easiest. I am surprised no one mentioned it earlier.

                                    – Abhishek Bhatia
                                    Feb 10 '16 at 1:49








                                  • 2





                                    On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

                                    – Andrew Franklin
                                    Jun 1 '16 at 18:30






                                  • 2





                                    Work beautifully for me in Ubuntu Desktop 16

                                    – Nam G VU
                                    Jul 21 '16 at 6:22














                                  17












                                  17








                                  17







                                  Quickest and easiest way without a doubt is Ctrl+L.



                                  This is the same for OS X on the terminal.






                                  share|improve this answer













                                  Quickest and easiest way without a doubt is Ctrl+L.



                                  This is the same for OS X on the terminal.







                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered Aug 28 '15 at 21:32









                                  Alex KAlex K

                                  6,86183151




                                  6,86183151








                                  • 3





                                    I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

                                    – Chris22
                                    Jan 11 '16 at 18:27






                                  • 3





                                    Yeah that is the easiest. I am surprised no one mentioned it earlier.

                                    – Abhishek Bhatia
                                    Feb 10 '16 at 1:49








                                  • 2





                                    On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

                                    – Andrew Franklin
                                    Jun 1 '16 at 18:30






                                  • 2





                                    Work beautifully for me in Ubuntu Desktop 16

                                    – Nam G VU
                                    Jul 21 '16 at 6:22














                                  • 3





                                    I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

                                    – Chris22
                                    Jan 11 '16 at 18:27






                                  • 3





                                    Yeah that is the easiest. I am surprised no one mentioned it earlier.

                                    – Abhishek Bhatia
                                    Feb 10 '16 at 1:49








                                  • 2





                                    On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

                                    – Andrew Franklin
                                    Jun 1 '16 at 18:30






                                  • 2





                                    Work beautifully for me in Ubuntu Desktop 16

                                    – Nam G VU
                                    Jul 21 '16 at 6:22








                                  3




                                  3





                                  I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

                                  – Chris22
                                  Jan 11 '16 at 18:27





                                  I tried this solution using upper or lowercase "L" with the ctrl key on windows 8.1. It doesn't work for me. I just open and close the shell window to clear it.

                                  – Chris22
                                  Jan 11 '16 at 18:27




                                  3




                                  3





                                  Yeah that is the easiest. I am surprised no one mentioned it earlier.

                                  – Abhishek Bhatia
                                  Feb 10 '16 at 1:49







                                  Yeah that is the easiest. I am surprised no one mentioned it earlier.

                                  – Abhishek Bhatia
                                  Feb 10 '16 at 1:49






                                  2




                                  2





                                  On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

                                  – Andrew Franklin
                                  Jun 1 '16 at 18:30





                                  On OS X, Ctrl+L will pad the terminal until the display is clear. You can still scroll up to see the history. Use Cmd+K to clear the display and printout history. A more complete list of OS X terminal hotkeys

                                  – Andrew Franklin
                                  Jun 1 '16 at 18:30




                                  2




                                  2





                                  Work beautifully for me in Ubuntu Desktop 16

                                  – Nam G VU
                                  Jul 21 '16 at 6:22





                                  Work beautifully for me in Ubuntu Desktop 16

                                  – Nam G VU
                                  Jul 21 '16 at 6:22











                                  12














                                  my way of doing this is to write a function like so:



                                  import os
                                  import subprocess

                                  def clear():
                                  if os.name in ('nt','dos'):
                                  subprocess.call("cls")
                                  elif os.name in ('linux','osx','posix'):
                                  subprocess.call("clear")
                                  else:
                                  print("n") * 120


                                  then call clear() to clear the screen.
                                  this works on windows, osx, linux, bsd... all OSes.






                                  share|improve this answer


























                                  • You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

                                    – rsanden
                                    Jul 24 '15 at 3:17











                                  • @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

                                    – MartinUbuntu
                                    Jul 24 '15 at 16:26











                                  • I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

                                    – rsanden
                                    Jul 24 '15 at 16:46











                                  • @rsanden added posix.

                                    – MartinUbuntu
                                    Aug 16 '15 at 10:41


















                                  12














                                  my way of doing this is to write a function like so:



                                  import os
                                  import subprocess

                                  def clear():
                                  if os.name in ('nt','dos'):
                                  subprocess.call("cls")
                                  elif os.name in ('linux','osx','posix'):
                                  subprocess.call("clear")
                                  else:
                                  print("n") * 120


                                  then call clear() to clear the screen.
                                  this works on windows, osx, linux, bsd... all OSes.






                                  share|improve this answer


























                                  • You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

                                    – rsanden
                                    Jul 24 '15 at 3:17











                                  • @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

                                    – MartinUbuntu
                                    Jul 24 '15 at 16:26











                                  • I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

                                    – rsanden
                                    Jul 24 '15 at 16:46











                                  • @rsanden added posix.

                                    – MartinUbuntu
                                    Aug 16 '15 at 10:41
















                                  12












                                  12








                                  12







                                  my way of doing this is to write a function like so:



                                  import os
                                  import subprocess

                                  def clear():
                                  if os.name in ('nt','dos'):
                                  subprocess.call("cls")
                                  elif os.name in ('linux','osx','posix'):
                                  subprocess.call("clear")
                                  else:
                                  print("n") * 120


                                  then call clear() to clear the screen.
                                  this works on windows, osx, linux, bsd... all OSes.






                                  share|improve this answer















                                  my way of doing this is to write a function like so:



                                  import os
                                  import subprocess

                                  def clear():
                                  if os.name in ('nt','dos'):
                                  subprocess.call("cls")
                                  elif os.name in ('linux','osx','posix'):
                                  subprocess.call("clear")
                                  else:
                                  print("n") * 120


                                  then call clear() to clear the screen.
                                  this works on windows, osx, linux, bsd... all OSes.







                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Oct 18 '17 at 15:22









                                  EgMusic

                                  6314




                                  6314










                                  answered Jun 3 '15 at 11:44









                                  MartinUbuntuMartinUbuntu

                                  13215




                                  13215













                                  • You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

                                    – rsanden
                                    Jul 24 '15 at 3:17











                                  • @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

                                    – MartinUbuntu
                                    Jul 24 '15 at 16:26











                                  • I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

                                    – rsanden
                                    Jul 24 '15 at 16:46











                                  • @rsanden added posix.

                                    – MartinUbuntu
                                    Aug 16 '15 at 10:41





















                                  • You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

                                    – rsanden
                                    Jul 24 '15 at 3:17











                                  • @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

                                    – MartinUbuntu
                                    Jul 24 '15 at 16:26











                                  • I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

                                    – rsanden
                                    Jul 24 '15 at 16:46











                                  • @rsanden added posix.

                                    – MartinUbuntu
                                    Aug 16 '15 at 10:41



















                                  You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

                                  – rsanden
                                  Jul 24 '15 at 3:17





                                  You might mean os.name in ('linux','osx'), and might also want to add 'posix' as well.

                                  – rsanden
                                  Jul 24 '15 at 3:17













                                  @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

                                  – MartinUbuntu
                                  Jul 24 '15 at 16:26





                                  @rsanden 'linux' and 'osx' covers pretty much all the OS's people ACTUALLY use.

                                  – MartinUbuntu
                                  Jul 24 '15 at 16:26













                                  I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

                                  – rsanden
                                  Jul 24 '15 at 16:46





                                  I am running Ubuntu 15.04, and os.name == 'posix' in both python 2.7.9 and 3.4.3

                                  – rsanden
                                  Jul 24 '15 at 16:46













                                  @rsanden added posix.

                                  – MartinUbuntu
                                  Aug 16 '15 at 10:41







                                  @rsanden added posix.

                                  – MartinUbuntu
                                  Aug 16 '15 at 10:41













                                  8














                                  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 Cls(object):
                                  def __repr__(self):
                                          os.system('cls')
                                  return ''


                                  The usage is quite simple:



                                  >>> cls = Cls()
                                  >>> cls # this will clear console.





                                  share|improve this answer





















                                  • 2





                                    I'd name it class cls.

                                    – martineau
                                    Oct 12 '10 at 17:25






                                  • 1





                                    I'd name the instance of the class Cls to be cls. cls = Cls()

                                    – Amol
                                    Dec 27 '11 at 5:47













                                  • Except that pollutes the initial namespace with two things instead of one...twice as much.

                                    – martineau
                                    Dec 27 '11 at 19:23













                                  • +1 An original way to make commands in Python.

                                    – Alba Mendez
                                    Jul 10 '12 at 12:14











                                  • @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

                                    – Alba Mendez
                                    Jul 17 '12 at 16:29
















                                  8














                                  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 Cls(object):
                                  def __repr__(self):
                                          os.system('cls')
                                  return ''


                                  The usage is quite simple:



                                  >>> cls = Cls()
                                  >>> cls # this will clear console.





                                  share|improve this answer





















                                  • 2





                                    I'd name it class cls.

                                    – martineau
                                    Oct 12 '10 at 17:25






                                  • 1





                                    I'd name the instance of the class Cls to be cls. cls = Cls()

                                    – Amol
                                    Dec 27 '11 at 5:47













                                  • Except that pollutes the initial namespace with two things instead of one...twice as much.

                                    – martineau
                                    Dec 27 '11 at 19:23













                                  • +1 An original way to make commands in Python.

                                    – Alba Mendez
                                    Jul 10 '12 at 12:14











                                  • @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

                                    – Alba Mendez
                                    Jul 17 '12 at 16:29














                                  8












                                  8








                                  8







                                  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 Cls(object):
                                  def __repr__(self):
                                          os.system('cls')
                                  return ''


                                  The usage is quite simple:



                                  >>> cls = Cls()
                                  >>> cls # this will clear console.





                                  share|improve this answer















                                  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 Cls(object):
                                  def __repr__(self):
                                          os.system('cls')
                                  return ''


                                  The usage is quite simple:



                                  >>> cls = Cls()
                                  >>> cls # this will clear console.






                                  share|improve this answer














                                  share|improve this answer



                                  share|improve this answer








                                  edited Feb 9 '12 at 8:57

























                                  answered Apr 17 '09 at 4:58









                                  AmolAmol

                                  8524




                                  8524








                                  • 2





                                    I'd name it class cls.

                                    – martineau
                                    Oct 12 '10 at 17:25






                                  • 1





                                    I'd name the instance of the class Cls to be cls. cls = Cls()

                                    – Amol
                                    Dec 27 '11 at 5:47













                                  • Except that pollutes the initial namespace with two things instead of one...twice as much.

                                    – martineau
                                    Dec 27 '11 at 19:23













                                  • +1 An original way to make commands in Python.

                                    – Alba Mendez
                                    Jul 10 '12 at 12:14











                                  • @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

                                    – Alba Mendez
                                    Jul 17 '12 at 16:29














                                  • 2





                                    I'd name it class cls.

                                    – martineau
                                    Oct 12 '10 at 17:25






                                  • 1





                                    I'd name the instance of the class Cls to be cls. cls = Cls()

                                    – Amol
                                    Dec 27 '11 at 5:47













                                  • Except that pollutes the initial namespace with two things instead of one...twice as much.

                                    – martineau
                                    Dec 27 '11 at 19:23













                                  • +1 An original way to make commands in Python.

                                    – Alba Mendez
                                    Jul 10 '12 at 12:14











                                  • @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

                                    – Alba Mendez
                                    Jul 17 '12 at 16:29








                                  2




                                  2





                                  I'd name it class cls.

                                  – martineau
                                  Oct 12 '10 at 17:25





                                  I'd name it class cls.

                                  – martineau
                                  Oct 12 '10 at 17:25




                                  1




                                  1





                                  I'd name the instance of the class Cls to be cls. cls = Cls()

                                  – Amol
                                  Dec 27 '11 at 5:47







                                  I'd name the instance of the class Cls to be cls. cls = Cls()

                                  – Amol
                                  Dec 27 '11 at 5:47















                                  Except that pollutes the initial namespace with two things instead of one...twice as much.

                                  – martineau
                                  Dec 27 '11 at 19:23







                                  Except that pollutes the initial namespace with two things instead of one...twice as much.

                                  – martineau
                                  Dec 27 '11 at 19:23















                                  +1 An original way to make commands in Python.

                                  – Alba Mendez
                                  Jul 10 '12 at 12:14





                                  +1 An original way to make commands in Python.

                                  – Alba Mendez
                                  Jul 10 '12 at 12:14













                                  @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

                                  – Alba Mendez
                                  Jul 17 '12 at 16:29





                                  @Amol I've used yours and others' techniques in my solution. You can do class cls and then cls=cls().

                                  – Alba Mendez
                                  Jul 17 '12 at 16:29











                                  5














                                  Here's the definitive solution that merges all other answers. Features:




                                  1. You can copy-paste the code into your shell or script.


                                  2. You can use it as you like:



                                    >>> clear()
                                    >>> -clear
                                    >>> clear # <- but this will only work on a shell



                                  3. You can import it as a module:



                                    >>> from clear import clear
                                    >>> -clear



                                  4. You can call it as a script:



                                    $ python clear.py


                                  5. It is truly multiplatform; if it can't recognize your system

                                    (ce, nt, dos or posix) it will fall back to printing blank lines.





                                  You can download the [full] file here: https://gist.github.com/3130325

                                  Or if you are just looking for the code:



                                  class clear:
                                  def __call__(self):
                                  import os
                                  if os.name==('ce','nt','dos'): os.system('cls')
                                  elif os.name=='posix': os.system('clear')
                                  else: print('n'*120)
                                  def __neg__(self): self()
                                  def __repr__(self):
                                  self();return ''

                                  clear=clear()





                                  share|improve this answer






























                                    5














                                    Here's the definitive solution that merges all other answers. Features:




                                    1. You can copy-paste the code into your shell or script.


                                    2. You can use it as you like:



                                      >>> clear()
                                      >>> -clear
                                      >>> clear # <- but this will only work on a shell



                                    3. You can import it as a module:



                                      >>> from clear import clear
                                      >>> -clear



                                    4. You can call it as a script:



                                      $ python clear.py


                                    5. It is truly multiplatform; if it can't recognize your system

                                      (ce, nt, dos or posix) it will fall back to printing blank lines.





                                    You can download the [full] file here: https://gist.github.com/3130325

                                    Or if you are just looking for the code:



                                    class clear:
                                    def __call__(self):
                                    import os
                                    if os.name==('ce','nt','dos'): os.system('cls')
                                    elif os.name=='posix': os.system('clear')
                                    else: print('n'*120)
                                    def __neg__(self): self()
                                    def __repr__(self):
                                    self();return ''

                                    clear=clear()





                                    share|improve this answer




























                                      5












                                      5








                                      5







                                      Here's the definitive solution that merges all other answers. Features:




                                      1. You can copy-paste the code into your shell or script.


                                      2. You can use it as you like:



                                        >>> clear()
                                        >>> -clear
                                        >>> clear # <- but this will only work on a shell



                                      3. You can import it as a module:



                                        >>> from clear import clear
                                        >>> -clear



                                      4. You can call it as a script:



                                        $ python clear.py


                                      5. It is truly multiplatform; if it can't recognize your system

                                        (ce, nt, dos or posix) it will fall back to printing blank lines.





                                      You can download the [full] file here: https://gist.github.com/3130325

                                      Or if you are just looking for the code:



                                      class clear:
                                      def __call__(self):
                                      import os
                                      if os.name==('ce','nt','dos'): os.system('cls')
                                      elif os.name=='posix': os.system('clear')
                                      else: print('n'*120)
                                      def __neg__(self): self()
                                      def __repr__(self):
                                      self();return ''

                                      clear=clear()





                                      share|improve this answer















                                      Here's the definitive solution that merges all other answers. Features:




                                      1. You can copy-paste the code into your shell or script.


                                      2. You can use it as you like:



                                        >>> clear()
                                        >>> -clear
                                        >>> clear # <- but this will only work on a shell



                                      3. You can import it as a module:



                                        >>> from clear import clear
                                        >>> -clear



                                      4. You can call it as a script:



                                        $ python clear.py


                                      5. It is truly multiplatform; if it can't recognize your system

                                        (ce, nt, dos or posix) it will fall back to printing blank lines.





                                      You can download the [full] file here: https://gist.github.com/3130325

                                      Or if you are just looking for the code:



                                      class clear:
                                      def __call__(self):
                                      import os
                                      if os.name==('ce','nt','dos'): os.system('cls')
                                      elif os.name=='posix': os.system('clear')
                                      else: print('n'*120)
                                      def __neg__(self): self()
                                      def __repr__(self):
                                      self();return ''

                                      clear=clear()






                                      share|improve this answer














                                      share|improve this answer



                                      share|improve this answer








                                      edited Nov 6 '12 at 12:52

























                                      answered Jul 17 '12 at 16:38









                                      Alba MendezAlba Mendez

                                      3,41612949




                                      3,41612949























                                          4














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






                                          share|improve this answer


























                                          • how do you do that on idle? Just close and reopen?

                                            – Andrea Ambu
                                            Feb 5 '09 at 23:08






                                          • 5





                                            No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

                                            – ridgerunner
                                            May 1 '11 at 14:58
















                                          4














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






                                          share|improve this answer


























                                          • how do you do that on idle? Just close and reopen?

                                            – Andrea Ambu
                                            Feb 5 '09 at 23:08






                                          • 5





                                            No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

                                            – ridgerunner
                                            May 1 '11 at 14:58














                                          4












                                          4








                                          4







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






                                          share|improve this answer















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







                                          share|improve this answer














                                          share|improve this answer



                                          share|improve this answer








                                          edited Apr 11 '14 at 23:36









                                          AndyG

                                          26.4k77096




                                          26.4k77096










                                          answered Feb 5 '09 at 23:03









                                          S.LottS.Lott

                                          317k66440717




                                          317k66440717













                                          • how do you do that on idle? Just close and reopen?

                                            – Andrea Ambu
                                            Feb 5 '09 at 23:08






                                          • 5





                                            No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

                                            – ridgerunner
                                            May 1 '11 at 14:58



















                                          • how do you do that on idle? Just close and reopen?

                                            – Andrea Ambu
                                            Feb 5 '09 at 23:08






                                          • 5





                                            No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

                                            – ridgerunner
                                            May 1 '11 at 14:58

















                                          how do you do that on idle? Just close and reopen?

                                          – Andrea Ambu
                                          Feb 5 '09 at 23:08





                                          how do you do that on idle? Just close and reopen?

                                          – Andrea Ambu
                                          Feb 5 '09 at 23:08




                                          5




                                          5





                                          No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

                                          – ridgerunner
                                          May 1 '11 at 14:58





                                          No, F6 does not reset the Idle console, however CTRL+F6 does. Unfortunately this does not clear the screen. D'oh! (Python Win32 Idle versions 2.6.2, 2.7.1, 3.2).

                                          – ridgerunner
                                          May 1 '11 at 14:58











                                          4














                                          I use iTerm and the native terminal app for Mac OS.



                                          I just press ⌘ + k






                                          share|improve this answer






























                                            4














                                            I use iTerm and the native terminal app for Mac OS.



                                            I just press ⌘ + k






                                            share|improve this answer




























                                              4












                                              4








                                              4







                                              I use iTerm and the native terminal app for Mac OS.



                                              I just press ⌘ + k






                                              share|improve this answer















                                              I use iTerm and the native terminal app for Mac OS.



                                              I just press ⌘ + k







                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              edited May 16 '16 at 13:48

























                                              answered May 9 '16 at 1:19









                                              Jorge E. HernándezJorge E. Hernández

                                              1,56911536




                                              1,56911536























                                                  4














                                                  Here's a cross platform (Windows / Linux / Mac / Probably others that you can add in the if check) version snippet I made combining information found in this question:



                                                  import os
                                                  clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
                                                  clear()


                                                  Same idea but with a spoon of syntactic sugar:



                                                  import subprocess   
                                                  clear = lambda: subprocess.call('cls||clear', shell=True)
                                                  clear()





                                                  share|improve this answer






























                                                    4














                                                    Here's a cross platform (Windows / Linux / Mac / Probably others that you can add in the if check) version snippet I made combining information found in this question:



                                                    import os
                                                    clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
                                                    clear()


                                                    Same idea but with a spoon of syntactic sugar:



                                                    import subprocess   
                                                    clear = lambda: subprocess.call('cls||clear', shell=True)
                                                    clear()





                                                    share|improve this answer




























                                                      4












                                                      4








                                                      4







                                                      Here's a cross platform (Windows / Linux / Mac / Probably others that you can add in the if check) version snippet I made combining information found in this question:



                                                      import os
                                                      clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
                                                      clear()


                                                      Same idea but with a spoon of syntactic sugar:



                                                      import subprocess   
                                                      clear = lambda: subprocess.call('cls||clear', shell=True)
                                                      clear()





                                                      share|improve this answer















                                                      Here's a cross platform (Windows / Linux / Mac / Probably others that you can add in the if check) version snippet I made combining information found in this question:



                                                      import os
                                                      clear = lambda: os.system('cls' if os.name=='nt' else 'clear')
                                                      clear()


                                                      Same idea but with a spoon of syntactic sugar:



                                                      import subprocess   
                                                      clear = lambda: subprocess.call('cls||clear', shell=True)
                                                      clear()






                                                      share|improve this answer














                                                      share|improve this answer



                                                      share|improve this answer








                                                      edited Nov 2 '18 at 11:01

























                                                      answered Jun 26 '18 at 12:51









                                                      PittoPitto

                                                      1,74211523




                                                      1,74211523























                                                          3














                                                          I'm not sure if Windows' "shell" supports this, but on Linux:



                                                          print "33[2J"



                                                          https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes



                                                          In my opinion calling cls with os is a bad idea generally. Imagine if I manage to change the cls or clear command on your system, and you run your script as admin or root.






                                                          share|improve this answer






























                                                            3














                                                            I'm not sure if Windows' "shell" supports this, but on Linux:



                                                            print "33[2J"



                                                            https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes



                                                            In my opinion calling cls with os is a bad idea generally. Imagine if I manage to change the cls or clear command on your system, and you run your script as admin or root.






                                                            share|improve this answer




























                                                              3












                                                              3








                                                              3







                                                              I'm not sure if Windows' "shell" supports this, but on Linux:



                                                              print "33[2J"



                                                              https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes



                                                              In my opinion calling cls with os is a bad idea generally. Imagine if I manage to change the cls or clear command on your system, and you run your script as admin or root.






                                                              share|improve this answer















                                                              I'm not sure if Windows' "shell" supports this, but on Linux:



                                                              print "33[2J"



                                                              https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_codes



                                                              In my opinion calling cls with os is a bad idea generally. Imagine if I manage to change the cls or clear command on your system, and you run your script as admin or root.







                                                              share|improve this answer














                                                              share|improve this answer



                                                              share|improve this answer








                                                              edited Jun 28 '15 at 21:32









                                                              Jerome

                                                              4,9102165121




                                                              4,9102165121










                                                              answered Jun 28 '15 at 20:31









                                                              Peter G. MarczisPeter G. Marczis

                                                              391




                                                              391























                                                                  2














                                                                  I'm using MINGW/BASH on Windows XP, SP3.



                                                                  (stick this in .pythonstartup)

                                                                  # My ctrl-l already kind of worked, but this might help someone else

                                                                  # leaves prompt at bottom of the window though...

                                                                  import readline

                                                                  readline.parse_and_bind('C-l: clear-screen')



                                                                  # This works in BASH because I have it in .inputrc as well, but for some

                                                                  # reason it gets dropped when I go into Python

                                                                  readline.parse_and_bind('C-y: kill-whole-line')





                                                                  I couldn't stand typing 'exit()' anymore and was delighted with martineau's/Triptych's tricks:



                                                                  I slightly doctored it though (stuck it in .pythonstartup)



                                                                  class exxxit():
                                                                  """Shortcut for exit() function, use 'x' now"""
                                                                  quit_now = exit # original object
                                                                  def __repr__(self):
                                                                  self.quit_now() # call original
                                                                  x = exxxit()




                                                                  Py2.7.1>help(x)
                                                                  Help on instance of exxxit in module __main__:

                                                                  class exxxit
                                                                  | Shortcut for exit() function, use 'x' now
                                                                  |
                                                                  | Methods defined here:
                                                                  |
                                                                  | __repr__(self)
                                                                  |
                                                                  | ----------------------------------------------------------------------
                                                                  | Data and other attributes defined here:
                                                                  |
                                                                  | quit_now = Use exit() or Ctrl-Z plus Return to exit





                                                                  share|improve this answer






























                                                                    2














                                                                    I'm using MINGW/BASH on Windows XP, SP3.



                                                                    (stick this in .pythonstartup)

                                                                    # My ctrl-l already kind of worked, but this might help someone else

                                                                    # leaves prompt at bottom of the window though...

                                                                    import readline

                                                                    readline.parse_and_bind('C-l: clear-screen')



                                                                    # This works in BASH because I have it in .inputrc as well, but for some

                                                                    # reason it gets dropped when I go into Python

                                                                    readline.parse_and_bind('C-y: kill-whole-line')





                                                                    I couldn't stand typing 'exit()' anymore and was delighted with martineau's/Triptych's tricks:



                                                                    I slightly doctored it though (stuck it in .pythonstartup)



                                                                    class exxxit():
                                                                    """Shortcut for exit() function, use 'x' now"""
                                                                    quit_now = exit # original object
                                                                    def __repr__(self):
                                                                    self.quit_now() # call original
                                                                    x = exxxit()




                                                                    Py2.7.1>help(x)
                                                                    Help on instance of exxxit in module __main__:

                                                                    class exxxit
                                                                    | Shortcut for exit() function, use 'x' now
                                                                    |
                                                                    | Methods defined here:
                                                                    |
                                                                    | __repr__(self)
                                                                    |
                                                                    | ----------------------------------------------------------------------
                                                                    | Data and other attributes defined here:
                                                                    |
                                                                    | quit_now = Use exit() or Ctrl-Z plus Return to exit





                                                                    share|improve this answer




























                                                                      2












                                                                      2








                                                                      2







                                                                      I'm using MINGW/BASH on Windows XP, SP3.



                                                                      (stick this in .pythonstartup)

                                                                      # My ctrl-l already kind of worked, but this might help someone else

                                                                      # leaves prompt at bottom of the window though...

                                                                      import readline

                                                                      readline.parse_and_bind('C-l: clear-screen')



                                                                      # This works in BASH because I have it in .inputrc as well, but for some

                                                                      # reason it gets dropped when I go into Python

                                                                      readline.parse_and_bind('C-y: kill-whole-line')





                                                                      I couldn't stand typing 'exit()' anymore and was delighted with martineau's/Triptych's tricks:



                                                                      I slightly doctored it though (stuck it in .pythonstartup)



                                                                      class exxxit():
                                                                      """Shortcut for exit() function, use 'x' now"""
                                                                      quit_now = exit # original object
                                                                      def __repr__(self):
                                                                      self.quit_now() # call original
                                                                      x = exxxit()




                                                                      Py2.7.1>help(x)
                                                                      Help on instance of exxxit in module __main__:

                                                                      class exxxit
                                                                      | Shortcut for exit() function, use 'x' now
                                                                      |
                                                                      | Methods defined here:
                                                                      |
                                                                      | __repr__(self)
                                                                      |
                                                                      | ----------------------------------------------------------------------
                                                                      | Data and other attributes defined here:
                                                                      |
                                                                      | quit_now = Use exit() or Ctrl-Z plus Return to exit





                                                                      share|improve this answer















                                                                      I'm using MINGW/BASH on Windows XP, SP3.



                                                                      (stick this in .pythonstartup)

                                                                      # My ctrl-l already kind of worked, but this might help someone else

                                                                      # leaves prompt at bottom of the window though...

                                                                      import readline

                                                                      readline.parse_and_bind('C-l: clear-screen')



                                                                      # This works in BASH because I have it in .inputrc as well, but for some

                                                                      # reason it gets dropped when I go into Python

                                                                      readline.parse_and_bind('C-y: kill-whole-line')





                                                                      I couldn't stand typing 'exit()' anymore and was delighted with martineau's/Triptych's tricks:



                                                                      I slightly doctored it though (stuck it in .pythonstartup)



                                                                      class exxxit():
                                                                      """Shortcut for exit() function, use 'x' now"""
                                                                      quit_now = exit # original object
                                                                      def __repr__(self):
                                                                      self.quit_now() # call original
                                                                      x = exxxit()




                                                                      Py2.7.1>help(x)
                                                                      Help on instance of exxxit in module __main__:

                                                                      class exxxit
                                                                      | Shortcut for exit() function, use 'x' now
                                                                      |
                                                                      | Methods defined here:
                                                                      |
                                                                      | __repr__(self)
                                                                      |
                                                                      | ----------------------------------------------------------------------
                                                                      | Data and other attributes defined here:
                                                                      |
                                                                      | quit_now = Use exit() or Ctrl-Z plus Return to exit






                                                                      share|improve this answer














                                                                      share|improve this answer



                                                                      share|improve this answer








                                                                      edited Nov 8 '11 at 21:57

























                                                                      answered Nov 8 '11 at 18:56









                                                                      AAAfarmclubAAAfarmclub

                                                                      1,152911




                                                                      1,152911























                                                                          2














                                                                          Here are two nice ways of doing that:



                                                                          1.



                                                                          import os

                                                                          # Clear Windows command prompt.
                                                                          if (os.name in ('ce', 'nt', 'dos')):
                                                                          os.system('cls')

                                                                          # Clear the Linux terminal.
                                                                          elif ('posix' in os.name):
                                                                          os.system('clear')


                                                                          2.



                                                                          import os

                                                                          def clear():
                                                                          if os.name == 'posix':
                                                                          os.system('clear')

                                                                          elif os.name in ('ce', 'nt', 'dos'):
                                                                          os.system('cls')


                                                                          clear()





                                                                          share|improve this answer


























                                                                          • If os.name is none of these, why not fall back to printing empty lines?

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:16











                                                                          • If it's Jython then I want you to know that os.name = 'java'

                                                                            – iChux
                                                                            Nov 29 '13 at 10:00











                                                                          • Looks good, does this mean that the program can become cross platform?

                                                                            – Luke
                                                                            Nov 16 '16 at 9:33
















                                                                          2














                                                                          Here are two nice ways of doing that:



                                                                          1.



                                                                          import os

                                                                          # Clear Windows command prompt.
                                                                          if (os.name in ('ce', 'nt', 'dos')):
                                                                          os.system('cls')

                                                                          # Clear the Linux terminal.
                                                                          elif ('posix' in os.name):
                                                                          os.system('clear')


                                                                          2.



                                                                          import os

                                                                          def clear():
                                                                          if os.name == 'posix':
                                                                          os.system('clear')

                                                                          elif os.name in ('ce', 'nt', 'dos'):
                                                                          os.system('cls')


                                                                          clear()





                                                                          share|improve this answer


























                                                                          • If os.name is none of these, why not fall back to printing empty lines?

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:16











                                                                          • If it's Jython then I want you to know that os.name = 'java'

                                                                            – iChux
                                                                            Nov 29 '13 at 10:00











                                                                          • Looks good, does this mean that the program can become cross platform?

                                                                            – Luke
                                                                            Nov 16 '16 at 9:33














                                                                          2












                                                                          2








                                                                          2







                                                                          Here are two nice ways of doing that:



                                                                          1.



                                                                          import os

                                                                          # Clear Windows command prompt.
                                                                          if (os.name in ('ce', 'nt', 'dos')):
                                                                          os.system('cls')

                                                                          # Clear the Linux terminal.
                                                                          elif ('posix' in os.name):
                                                                          os.system('clear')


                                                                          2.



                                                                          import os

                                                                          def clear():
                                                                          if os.name == 'posix':
                                                                          os.system('clear')

                                                                          elif os.name in ('ce', 'nt', 'dos'):
                                                                          os.system('cls')


                                                                          clear()





                                                                          share|improve this answer















                                                                          Here are two nice ways of doing that:



                                                                          1.



                                                                          import os

                                                                          # Clear Windows command prompt.
                                                                          if (os.name in ('ce', 'nt', 'dos')):
                                                                          os.system('cls')

                                                                          # Clear the Linux terminal.
                                                                          elif ('posix' in os.name):
                                                                          os.system('clear')


                                                                          2.



                                                                          import os

                                                                          def clear():
                                                                          if os.name == 'posix':
                                                                          os.system('clear')

                                                                          elif os.name in ('ce', 'nt', 'dos'):
                                                                          os.system('cls')


                                                                          clear()






                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Mar 28 '16 at 14:12









                                                                          James Jithin

                                                                          7,25032243




                                                                          7,25032243










                                                                          answered Sep 1 '11 at 2:13









                                                                          userenduserend

                                                                          1,4511119




                                                                          1,4511119













                                                                          • If os.name is none of these, why not fall back to printing empty lines?

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:16











                                                                          • If it's Jython then I want you to know that os.name = 'java'

                                                                            – iChux
                                                                            Nov 29 '13 at 10:00











                                                                          • Looks good, does this mean that the program can become cross platform?

                                                                            – Luke
                                                                            Nov 16 '16 at 9:33



















                                                                          • If os.name is none of these, why not fall back to printing empty lines?

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:16











                                                                          • If it's Jython then I want you to know that os.name = 'java'

                                                                            – iChux
                                                                            Nov 29 '13 at 10:00











                                                                          • Looks good, does this mean that the program can become cross platform?

                                                                            – Luke
                                                                            Nov 16 '16 at 9:33

















                                                                          If os.name is none of these, why not fall back to printing empty lines?

                                                                          – Alba Mendez
                                                                          Jul 10 '12 at 12:16





                                                                          If os.name is none of these, why not fall back to printing empty lines?

                                                                          – Alba Mendez
                                                                          Jul 10 '12 at 12:16













                                                                          If it's Jython then I want you to know that os.name = 'java'

                                                                          – iChux
                                                                          Nov 29 '13 at 10:00





                                                                          If it's Jython then I want you to know that os.name = 'java'

                                                                          – iChux
                                                                          Nov 29 '13 at 10:00













                                                                          Looks good, does this mean that the program can become cross platform?

                                                                          – Luke
                                                                          Nov 16 '16 at 9:33





                                                                          Looks good, does this mean that the program can become cross platform?

                                                                          – Luke
                                                                          Nov 16 '16 at 9:33











                                                                          1














                                                                          How about this for a clear



                                                                          - os.system('cls')


                                                                          That is about as short as could be!






                                                                          share|improve this answer


























                                                                          • yes, but not multiplatform and you have to retype it every time you want to clear the screen.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:22
















                                                                          1














                                                                          How about this for a clear



                                                                          - os.system('cls')


                                                                          That is about as short as could be!






                                                                          share|improve this answer


























                                                                          • yes, but not multiplatform and you have to retype it every time you want to clear the screen.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:22














                                                                          1












                                                                          1








                                                                          1







                                                                          How about this for a clear



                                                                          - os.system('cls')


                                                                          That is about as short as could be!






                                                                          share|improve this answer















                                                                          How about this for a clear



                                                                          - os.system('cls')


                                                                          That is about as short as could be!







                                                                          share|improve this answer














                                                                          share|improve this answer



                                                                          share|improve this answer








                                                                          edited Nov 2 '11 at 21:56









                                                                          Bo Persson

                                                                          78k17118184




                                                                          78k17118184










                                                                          answered Nov 2 '11 at 21:48









                                                                          Dennis KeanDennis Kean

                                                                          191




                                                                          191













                                                                          • yes, but not multiplatform and you have to retype it every time you want to clear the screen.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:22



















                                                                          • yes, but not multiplatform and you have to retype it every time you want to clear the screen.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:22

















                                                                          yes, but not multiplatform and you have to retype it every time you want to clear the screen.

                                                                          – Alba Mendez
                                                                          Jul 10 '12 at 12:22





                                                                          yes, but not multiplatform and you have to retype it every time you want to clear the screen.

                                                                          – Alba Mendez
                                                                          Jul 10 '12 at 12:22











                                                                          1














                                                                          The OS command clear in Linux and cls in Windows outputs a "magic string" which you can just print. To get the string, execute the command with popen and save it in a variable for later use:



                                                                          from os import popen
                                                                          with popen('clear') as f:
                                                                          clear = f.read()

                                                                          print clear


                                                                          On my machine the string is 'x1b[Hx1b[2J'.






                                                                          share|improve this answer



















                                                                          • 1





                                                                            1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:20











                                                                          • Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

                                                                            – DenMark
                                                                            Jul 25 '17 at 7:04
















                                                                          1














                                                                          The OS command clear in Linux and cls in Windows outputs a "magic string" which you can just print. To get the string, execute the command with popen and save it in a variable for later use:



                                                                          from os import popen
                                                                          with popen('clear') as f:
                                                                          clear = f.read()

                                                                          print clear


                                                                          On my machine the string is 'x1b[Hx1b[2J'.






                                                                          share|improve this answer



















                                                                          • 1





                                                                            1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:20











                                                                          • Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

                                                                            – DenMark
                                                                            Jul 25 '17 at 7:04














                                                                          1












                                                                          1








                                                                          1







                                                                          The OS command clear in Linux and cls in Windows outputs a "magic string" which you can just print. To get the string, execute the command with popen and save it in a variable for later use:



                                                                          from os import popen
                                                                          with popen('clear') as f:
                                                                          clear = f.read()

                                                                          print clear


                                                                          On my machine the string is 'x1b[Hx1b[2J'.






                                                                          share|improve this answer













                                                                          The OS command clear in Linux and cls in Windows outputs a "magic string" which you can just print. To get the string, execute the command with popen and save it in a variable for later use:



                                                                          from os import popen
                                                                          with popen('clear') as f:
                                                                          clear = f.read()

                                                                          print clear


                                                                          On my machine the string is 'x1b[Hx1b[2J'.







                                                                          share|improve this answer












                                                                          share|improve this answer



                                                                          share|improve this answer










                                                                          answered Dec 11 '11 at 11:19









                                                                          larsrlarsr

                                                                          3,1421228




                                                                          3,1421228








                                                                          • 1





                                                                            1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:20











                                                                          • Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

                                                                            – DenMark
                                                                            Jul 25 '17 at 7:04














                                                                          • 1





                                                                            1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

                                                                            – Alba Mendez
                                                                            Jul 10 '12 at 12:20











                                                                          • Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

                                                                            – DenMark
                                                                            Jul 25 '17 at 7:04








                                                                          1




                                                                          1





                                                                          1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

                                                                          – Alba Mendez
                                                                          Jul 10 '12 at 12:20





                                                                          1) That magic string is an ANSI sequence. x1b[H means "move the cursor to the top-left corner", x1b[2J means "clear all the screen". 2) In windows, ANSI is not recognized so probably there isn't any magic string.

                                                                          – Alba Mendez
                                                                          Jul 10 '12 at 12:20













                                                                          Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

                                                                          – DenMark
                                                                          Jul 25 '17 at 7:04





                                                                          Cool! Also, for python3 print('x1b[Hx1b[2J', end=''); can help avoid the new line in front.

                                                                          – DenMark
                                                                          Jul 25 '17 at 7:04











                                                                          1














                                                                          I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:



                                                                          Open shell / Create new document / Create function as follows:



                                                                          def clear():
                                                                          print('n' * 50)


                                                                          Save it inside the lib folder in you python directory (mine is C:Python33Lib)
                                                                          Next time you nedd to clear your console just call the function with:



                                                                          clear()


                                                                          that's it.
                                                                          PS: you can name you function anyway you want. Iv' seen people using "wiper" "wipe" and variations.






                                                                          share|improve this answer






























                                                                            1














                                                                            I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:



                                                                            Open shell / Create new document / Create function as follows:



                                                                            def clear():
                                                                            print('n' * 50)


                                                                            Save it inside the lib folder in you python directory (mine is C:Python33Lib)
                                                                            Next time you nedd to clear your console just call the function with:



                                                                            clear()


                                                                            that's it.
                                                                            PS: you can name you function anyway you want. Iv' seen people using "wiper" "wipe" and variations.






                                                                            share|improve this answer




























                                                                              1












                                                                              1








                                                                              1







                                                                              I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:



                                                                              Open shell / Create new document / Create function as follows:



                                                                              def clear():
                                                                              print('n' * 50)


                                                                              Save it inside the lib folder in you python directory (mine is C:Python33Lib)
                                                                              Next time you nedd to clear your console just call the function with:



                                                                              clear()


                                                                              that's it.
                                                                              PS: you can name you function anyway you want. Iv' seen people using "wiper" "wipe" and variations.






                                                                              share|improve this answer















                                                                              I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints:



                                                                              Open shell / Create new document / Create function as follows:



                                                                              def clear():
                                                                              print('n' * 50)


                                                                              Save it inside the lib folder in you python directory (mine is C:Python33Lib)
                                                                              Next time you nedd to clear your console just call the function with:



                                                                              clear()


                                                                              that's it.
                                                                              PS: you can name you function anyway you want. Iv' seen people using "wiper" "wipe" and variations.







                                                                              share|improve this answer














                                                                              share|improve this answer



                                                                              share|improve this answer








                                                                              edited Mar 25 '13 at 20:00









                                                                              Kirk

                                                                              13.5k176399




                                                                              13.5k176399










                                                                              answered Mar 25 '13 at 19:41









                                                                              AdrianaAdriana

                                                                              271




                                                                              271























                                                                                  1














                                                                                  I found the simplest way is just to close the window and run a module/script to reopen the shell.






                                                                                  share|improve this answer




























                                                                                    1














                                                                                    I found the simplest way is just to close the window and run a module/script to reopen the shell.






                                                                                    share|improve this answer


























                                                                                      1












                                                                                      1








                                                                                      1







                                                                                      I found the simplest way is just to close the window and run a module/script to reopen the shell.






                                                                                      share|improve this answer













                                                                                      I found the simplest way is just to close the window and run a module/script to reopen the shell.







                                                                                      share|improve this answer












                                                                                      share|improve this answer



                                                                                      share|improve this answer










                                                                                      answered Sep 17 '13 at 10:00









                                                                                      SeymourSeymour

                                                                                      191




                                                                                      191























                                                                                          1














                                                                                          just use this..



                                                                                          print 'n'*1000






                                                                                          share|improve this answer




























                                                                                            1














                                                                                            just use this..



                                                                                            print 'n'*1000






                                                                                            share|improve this answer


























                                                                                              1












                                                                                              1








                                                                                              1







                                                                                              just use this..



                                                                                              print 'n'*1000






                                                                                              share|improve this answer













                                                                                              just use this..



                                                                                              print 'n'*1000







                                                                                              share|improve this answer












                                                                                              share|improve this answer



                                                                                              share|improve this answer










                                                                                              answered May 19 '14 at 6:15









                                                                                              user1474157user1474157

                                                                                              1311311




                                                                                              1311311























                                                                                                  1














                                                                                                  I might be late to the part but here is a very easy way to do it



                                                                                                  Type:



                                                                                                  def cls():
                                                                                                  os.system("cls")


                                                                                                  So what ever you want to clear the screen just type in your code



                                                                                                  cls()


                                                                                                  Best way possible! (Credit : https://www.youtube.com/watch?annotation_id=annotation_3770292585&feature=iv&src_vid=bguKhMnvmb8&v=LtGEp9c6Z-U)






                                                                                                  share|improve this answer




























                                                                                                    1














                                                                                                    I might be late to the part but here is a very easy way to do it



                                                                                                    Type:



                                                                                                    def cls():
                                                                                                    os.system("cls")


                                                                                                    So what ever you want to clear the screen just type in your code



                                                                                                    cls()


                                                                                                    Best way possible! (Credit : https://www.youtube.com/watch?annotation_id=annotation_3770292585&feature=iv&src_vid=bguKhMnvmb8&v=LtGEp9c6Z-U)






                                                                                                    share|improve this answer


























                                                                                                      1












                                                                                                      1








                                                                                                      1







                                                                                                      I might be late to the part but here is a very easy way to do it



                                                                                                      Type:



                                                                                                      def cls():
                                                                                                      os.system("cls")


                                                                                                      So what ever you want to clear the screen just type in your code



                                                                                                      cls()


                                                                                                      Best way possible! (Credit : https://www.youtube.com/watch?annotation_id=annotation_3770292585&feature=iv&src_vid=bguKhMnvmb8&v=LtGEp9c6Z-U)






                                                                                                      share|improve this answer













                                                                                                      I might be late to the part but here is a very easy way to do it



                                                                                                      Type:



                                                                                                      def cls():
                                                                                                      os.system("cls")


                                                                                                      So what ever you want to clear the screen just type in your code



                                                                                                      cls()


                                                                                                      Best way possible! (Credit : https://www.youtube.com/watch?annotation_id=annotation_3770292585&feature=iv&src_vid=bguKhMnvmb8&v=LtGEp9c6Z-U)







                                                                                                      share|improve this answer












                                                                                                      share|improve this answer



                                                                                                      share|improve this answer










                                                                                                      answered Aug 17 '16 at 0:05









                                                                                                      stallion093stallion093

                                                                                                      213




                                                                                                      213























                                                                                                          1














                                                                                                          This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return >>> to the top left corner.



                                                                                                          print("33[H33[J")





                                                                                                          share|improve this answer



















                                                                                                          • 2





                                                                                                            It does not work with Windows Python 3.7 version.

                                                                                                            – Gunny KC
                                                                                                            Jul 7 '18 at 5:00
















                                                                                                          1














                                                                                                          This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return >>> to the top left corner.



                                                                                                          print("33[H33[J")





                                                                                                          share|improve this answer



















                                                                                                          • 2





                                                                                                            It does not work with Windows Python 3.7 version.

                                                                                                            – Gunny KC
                                                                                                            Jul 7 '18 at 5:00














                                                                                                          1












                                                                                                          1








                                                                                                          1







                                                                                                          This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return >>> to the top left corner.



                                                                                                          print("33[H33[J")





                                                                                                          share|improve this answer













                                                                                                          This is the simplest thing you can do and it doesn't require any additional libraries. It will clear the screen and return >>> to the top left corner.



                                                                                                          print("33[H33[J")






                                                                                                          share|improve this answer












                                                                                                          share|improve this answer



                                                                                                          share|improve this answer










                                                                                                          answered May 28 '18 at 6:54









                                                                                                          Denis RasulevDenis Rasulev

                                                                                                          1,19211621




                                                                                                          1,19211621








                                                                                                          • 2





                                                                                                            It does not work with Windows Python 3.7 version.

                                                                                                            – Gunny KC
                                                                                                            Jul 7 '18 at 5:00














                                                                                                          • 2





                                                                                                            It does not work with Windows Python 3.7 version.

                                                                                                            – Gunny KC
                                                                                                            Jul 7 '18 at 5:00








                                                                                                          2




                                                                                                          2





                                                                                                          It does not work with Windows Python 3.7 version.

                                                                                                          – Gunny KC
                                                                                                          Jul 7 '18 at 5:00





                                                                                                          It does not work with Windows Python 3.7 version.

                                                                                                          – Gunny KC
                                                                                                          Jul 7 '18 at 5:00











                                                                                                          0














                                                                                                          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.






                                                                                                          share|improve this answer




























                                                                                                            0














                                                                                                            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.






                                                                                                            share|improve this answer


























                                                                                                              0












                                                                                                              0








                                                                                                              0







                                                                                                              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.






                                                                                                              share|improve this answer













                                                                                                              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.







                                                                                                              share|improve this answer












                                                                                                              share|improve this answer



                                                                                                              share|improve this answer










                                                                                                              answered Feb 5 '09 at 23:51









                                                                                                              Andrea AmbuAndrea Ambu

                                                                                                              17.1k124774




                                                                                                              17.1k124774























                                                                                                                  0














                                                                                                                  This should be cross platform, and also uses the preferred subprocess.call instead of os.system as per the os.system docs. Should work in Python >= 2.4.



                                                                                                                  import subprocess
                                                                                                                  import os

                                                                                                                  if os.name == 'nt':
                                                                                                                  def clearscreen():
                                                                                                                  subprocess.call("cls", shell=True)
                                                                                                                  return
                                                                                                                  else:
                                                                                                                  def clearscreen():
                                                                                                                  subprocess.call("clear", shell=True)
                                                                                                                  return





                                                                                                                  share|improve this answer




























                                                                                                                    0














                                                                                                                    This should be cross platform, and also uses the preferred subprocess.call instead of os.system as per the os.system docs. Should work in Python >= 2.4.



                                                                                                                    import subprocess
                                                                                                                    import os

                                                                                                                    if os.name == 'nt':
                                                                                                                    def clearscreen():
                                                                                                                    subprocess.call("cls", shell=True)
                                                                                                                    return
                                                                                                                    else:
                                                                                                                    def clearscreen():
                                                                                                                    subprocess.call("clear", shell=True)
                                                                                                                    return





                                                                                                                    share|improve this answer


























                                                                                                                      0












                                                                                                                      0








                                                                                                                      0







                                                                                                                      This should be cross platform, and also uses the preferred subprocess.call instead of os.system as per the os.system docs. Should work in Python >= 2.4.



                                                                                                                      import subprocess
                                                                                                                      import os

                                                                                                                      if os.name == 'nt':
                                                                                                                      def clearscreen():
                                                                                                                      subprocess.call("cls", shell=True)
                                                                                                                      return
                                                                                                                      else:
                                                                                                                      def clearscreen():
                                                                                                                      subprocess.call("clear", shell=True)
                                                                                                                      return





                                                                                                                      share|improve this answer













                                                                                                                      This should be cross platform, and also uses the preferred subprocess.call instead of os.system as per the os.system docs. Should work in Python >= 2.4.



                                                                                                                      import subprocess
                                                                                                                      import os

                                                                                                                      if os.name == 'nt':
                                                                                                                      def clearscreen():
                                                                                                                      subprocess.call("cls", shell=True)
                                                                                                                      return
                                                                                                                      else:
                                                                                                                      def clearscreen():
                                                                                                                      subprocess.call("clear", shell=True)
                                                                                                                      return






                                                                                                                      share|improve this answer












                                                                                                                      share|improve this answer



                                                                                                                      share|improve this answer










                                                                                                                      answered Mar 20 '11 at 14:51









                                                                                                                      AcornAcorn

                                                                                                                      31.7k18110150




                                                                                                                      31.7k18110150























                                                                                                                          0














                                                                                                                          OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". Hope this helps someone out there!






                                                                                                                          share|improve this answer




























                                                                                                                            0














                                                                                                                            OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". Hope this helps someone out there!






                                                                                                                            share|improve this answer


























                                                                                                                              0












                                                                                                                              0








                                                                                                                              0







                                                                                                                              OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". Hope this helps someone out there!






                                                                                                                              share|improve this answer













                                                                                                                              OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". Hope this helps someone out there!







                                                                                                                              share|improve this answer












                                                                                                                              share|improve this answer



                                                                                                                              share|improve this answer










                                                                                                                              answered Jul 8 '13 at 21:24









                                                                                                                              guest12345guest12345

                                                                                                                              91




                                                                                                                              91























                                                                                                                                  0














                                                                                                                                  >>> ' '*80*25


                                                                                                                                  UPDATE: 80x25 is unlikely to be the size of console windows, so to get the real console dimensions, use functions from pager module. Python doesn't provide anything similar from core distribution.



                                                                                                                                  >>> from pager import getheight
                                                                                                                                  >>> 'n' * getheight()





                                                                                                                                  share|improve this answer


























                                                                                                                                  • That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                                                                                                                                    – Alba Mendez
                                                                                                                                    Jul 10 '12 at 12:24











                                                                                                                                  • Shrt ansrs rck!

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 16 '12 at 10:54













                                                                                                                                  • It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                                                                                                                                    – aramis
                                                                                                                                    Jul 17 '12 at 22:34











                                                                                                                                  • Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 18 '12 at 8:43
















                                                                                                                                  0














                                                                                                                                  >>> ' '*80*25


                                                                                                                                  UPDATE: 80x25 is unlikely to be the size of console windows, so to get the real console dimensions, use functions from pager module. Python doesn't provide anything similar from core distribution.



                                                                                                                                  >>> from pager import getheight
                                                                                                                                  >>> 'n' * getheight()





                                                                                                                                  share|improve this answer


























                                                                                                                                  • That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                                                                                                                                    – Alba Mendez
                                                                                                                                    Jul 10 '12 at 12:24











                                                                                                                                  • Shrt ansrs rck!

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 16 '12 at 10:54













                                                                                                                                  • It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                                                                                                                                    – aramis
                                                                                                                                    Jul 17 '12 at 22:34











                                                                                                                                  • Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 18 '12 at 8:43














                                                                                                                                  0












                                                                                                                                  0








                                                                                                                                  0







                                                                                                                                  >>> ' '*80*25


                                                                                                                                  UPDATE: 80x25 is unlikely to be the size of console windows, so to get the real console dimensions, use functions from pager module. Python doesn't provide anything similar from core distribution.



                                                                                                                                  >>> from pager import getheight
                                                                                                                                  >>> 'n' * getheight()





                                                                                                                                  share|improve this answer















                                                                                                                                  >>> ' '*80*25


                                                                                                                                  UPDATE: 80x25 is unlikely to be the size of console windows, so to get the real console dimensions, use functions from pager module. Python doesn't provide anything similar from core distribution.



                                                                                                                                  >>> from pager import getheight
                                                                                                                                  >>> 'n' * getheight()






                                                                                                                                  share|improve this answer














                                                                                                                                  share|improve this answer



                                                                                                                                  share|improve this answer








                                                                                                                                  edited Oct 30 '13 at 4:48

























                                                                                                                                  answered Mar 17 '12 at 17:08









                                                                                                                                  anatoly techtonikanatoly techtonik

                                                                                                                                  11.8k584101




                                                                                                                                  11.8k584101













                                                                                                                                  • That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                                                                                                                                    – Alba Mendez
                                                                                                                                    Jul 10 '12 at 12:24











                                                                                                                                  • Shrt ansrs rck!

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 16 '12 at 10:54













                                                                                                                                  • It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                                                                                                                                    – aramis
                                                                                                                                    Jul 17 '12 at 22:34











                                                                                                                                  • Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 18 '12 at 8:43



















                                                                                                                                  • That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                                                                                                                                    – Alba Mendez
                                                                                                                                    Jul 10 '12 at 12:24











                                                                                                                                  • Shrt ansrs rck!

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 16 '12 at 10:54













                                                                                                                                  • It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                                                                                                                                    – aramis
                                                                                                                                    Jul 17 '12 at 22:34











                                                                                                                                  • Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                                                                                                                                    – anatoly techtonik
                                                                                                                                    Jul 18 '12 at 8:43

















                                                                                                                                  That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                                                                                                                                  – Alba Mendez
                                                                                                                                  Jul 10 '12 at 12:24





                                                                                                                                  That rule is there to avoid simply posting code. It tries to make people explain his answer, not just giving code.

                                                                                                                                  – Alba Mendez
                                                                                                                                  Jul 10 '12 at 12:24













                                                                                                                                  Shrt ansrs rck!

                                                                                                                                  – anatoly techtonik
                                                                                                                                  Jul 16 '12 at 10:54







                                                                                                                                  Shrt ansrs rck!

                                                                                                                                  – anatoly techtonik
                                                                                                                                  Jul 16 '12 at 10:54















                                                                                                                                  It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                                                                                                                                  – aramis
                                                                                                                                  Jul 17 '12 at 22:34





                                                                                                                                  It's not a good answer anyway - it's just printing a string of 80*25 spaces... which only works if the console is 2000 characters or smaller (such as 80x25, or 100x20... but the console often winds up 120x50 on my machine.

                                                                                                                                  – aramis
                                                                                                                                  Jul 17 '12 at 22:34













                                                                                                                                  Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                                                                                                                                  – anatoly techtonik
                                                                                                                                  Jul 18 '12 at 8:43





                                                                                                                                  Use pypi.python.org/pypi/pager getwidth/getheight to detect console parameters.

                                                                                                                                  – anatoly techtonik
                                                                                                                                  Jul 18 '12 at 8:43











                                                                                                                                  0














                                                                                                                                  Magic strings are mentioned above - I believe they come from the terminfo database:



                                                                                                                                  http://www.google.com/?q=x#q=terminfo



                                                                                                                                  http://www.google.com/?q=x#q=tput+command+in+unix



                                                                                                                                  $ tput clear| od -t x1z
                                                                                                                                  0000000 1b 5b 48 1b 5b 32 4a >.[H.[2J<
                                                                                                                                  0000007






                                                                                                                                  share|improve this answer




























                                                                                                                                    0














                                                                                                                                    Magic strings are mentioned above - I believe they come from the terminfo database:



                                                                                                                                    http://www.google.com/?q=x#q=terminfo



                                                                                                                                    http://www.google.com/?q=x#q=tput+command+in+unix



                                                                                                                                    $ tput clear| od -t x1z
                                                                                                                                    0000000 1b 5b 48 1b 5b 32 4a >.[H.[2J<
                                                                                                                                    0000007






                                                                                                                                    share|improve this answer


























                                                                                                                                      0












                                                                                                                                      0








                                                                                                                                      0







                                                                                                                                      Magic strings are mentioned above - I believe they come from the terminfo database:



                                                                                                                                      http://www.google.com/?q=x#q=terminfo



                                                                                                                                      http://www.google.com/?q=x#q=tput+command+in+unix



                                                                                                                                      $ tput clear| od -t x1z
                                                                                                                                      0000000 1b 5b 48 1b 5b 32 4a >.[H.[2J<
                                                                                                                                      0000007






                                                                                                                                      share|improve this answer













                                                                                                                                      Magic strings are mentioned above - I believe they come from the terminfo database:



                                                                                                                                      http://www.google.com/?q=x#q=terminfo



                                                                                                                                      http://www.google.com/?q=x#q=tput+command+in+unix



                                                                                                                                      $ tput clear| od -t x1z
                                                                                                                                      0000000 1b 5b 48 1b 5b 32 4a >.[H.[2J<
                                                                                                                                      0000007







                                                                                                                                      share|improve this answer












                                                                                                                                      share|improve this answer



                                                                                                                                      share|improve this answer










                                                                                                                                      answered Jan 18 '14 at 21:54









                                                                                                                                      tput-guesttput-guest

                                                                                                                                      91




                                                                                                                                      91























                                                                                                                                          0














                                                                                                                                          I am using Spyder (Python 2.7) and to clean the interpreter console I use either



                                                                                                                                          %clear



                                                                                                                                          that forces the command line to go to the top and I will not see the previous old commands.



                                                                                                                                          or I click "option" on the Console environment and select "Restart kernel" that removes everything.






                                                                                                                                          share|improve this answer




























                                                                                                                                            0














                                                                                                                                            I am using Spyder (Python 2.7) and to clean the interpreter console I use either



                                                                                                                                            %clear



                                                                                                                                            that forces the command line to go to the top and I will not see the previous old commands.



                                                                                                                                            or I click "option" on the Console environment and select "Restart kernel" that removes everything.






                                                                                                                                            share|improve this answer


























                                                                                                                                              0












                                                                                                                                              0








                                                                                                                                              0







                                                                                                                                              I am using Spyder (Python 2.7) and to clean the interpreter console I use either



                                                                                                                                              %clear



                                                                                                                                              that forces the command line to go to the top and I will not see the previous old commands.



                                                                                                                                              or I click "option" on the Console environment and select "Restart kernel" that removes everything.






                                                                                                                                              share|improve this answer













                                                                                                                                              I am using Spyder (Python 2.7) and to clean the interpreter console I use either



                                                                                                                                              %clear



                                                                                                                                              that forces the command line to go to the top and I will not see the previous old commands.



                                                                                                                                              or I click "option" on the Console environment and select "Restart kernel" that removes everything.







                                                                                                                                              share|improve this answer












                                                                                                                                              share|improve this answer



                                                                                                                                              share|improve this answer










                                                                                                                                              answered Apr 8 '15 at 16:35









                                                                                                                                              YounessYouness

                                                                                                                                              12




                                                                                                                                              12























                                                                                                                                                  0














                                                                                                                                                  Just enter



                                                                                                                                                  import os
                                                                                                                                                  os.system('cls') # Windows
                                                                                                                                                  os.system('clear') # Linux, Unix, Mac OS X





                                                                                                                                                  share|improve this answer




























                                                                                                                                                    0














                                                                                                                                                    Just enter



                                                                                                                                                    import os
                                                                                                                                                    os.system('cls') # Windows
                                                                                                                                                    os.system('clear') # Linux, Unix, Mac OS X





                                                                                                                                                    share|improve this answer


























                                                                                                                                                      0












                                                                                                                                                      0








                                                                                                                                                      0







                                                                                                                                                      Just enter



                                                                                                                                                      import os
                                                                                                                                                      os.system('cls') # Windows
                                                                                                                                                      os.system('clear') # Linux, Unix, Mac OS X





                                                                                                                                                      share|improve this answer













                                                                                                                                                      Just enter



                                                                                                                                                      import os
                                                                                                                                                      os.system('cls') # Windows
                                                                                                                                                      os.system('clear') # Linux, Unix, Mac OS X






                                                                                                                                                      share|improve this answer












                                                                                                                                                      share|improve this answer



                                                                                                                                                      share|improve this answer










                                                                                                                                                      answered Apr 10 '18 at 22:00









                                                                                                                                                      AnynomousAnynomous

                                                                                                                                                      99211




                                                                                                                                                      99211























                                                                                                                                                          0














                                                                                                                                                          The easiest way
                                                                                                                                                          `>>> import os






                                                                                                                                                          clear = lambda: os.system('clear')
                                                                                                                                                          clear()`









                                                                                                                                                          share|improve this answer




























                                                                                                                                                            0














                                                                                                                                                            The easiest way
                                                                                                                                                            `>>> import os






                                                                                                                                                            clear = lambda: os.system('clear')
                                                                                                                                                            clear()`









                                                                                                                                                            share|improve this answer


























                                                                                                                                                              0












                                                                                                                                                              0








                                                                                                                                                              0







                                                                                                                                                              The easiest way
                                                                                                                                                              `>>> import os






                                                                                                                                                              clear = lambda: os.system('clear')
                                                                                                                                                              clear()`









                                                                                                                                                              share|improve this answer













                                                                                                                                                              The easiest way
                                                                                                                                                              `>>> import os






                                                                                                                                                              clear = lambda: os.system('clear')
                                                                                                                                                              clear()`










                                                                                                                                                              share|improve this answer












                                                                                                                                                              share|improve this answer



                                                                                                                                                              share|improve this answer










                                                                                                                                                              answered Jul 19 '18 at 6:17









                                                                                                                                                              Bhaskar MishraBhaskar Mishra

                                                                                                                                                              111




                                                                                                                                                              111






















                                                                                                                                                                  1 2
                                                                                                                                                                  next




                                                                                                                                                                  protected by vsoftco Oct 18 '16 at 2:17



                                                                                                                                                                  Thank you for your interest in this question.
                                                                                                                                                                  Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                                                                                                                                                                  Would you like to answer one of these unanswered questions instead?



                                                                                                                                                                  Popular posts from this blog

                                                                                                                                                                  Guess what letter conforming each word

                                                                                                                                                                  Port of Spain

                                                                                                                                                                  Run scheduled task as local user group (not BUILTIN)