How to clear the interpreter console?

后端 未结 30 2199
自闭症患者
自闭症患者 2020-11-21 18:15

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

相关标签:
30条回答
  • 2020-11-21 18:35

    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:\Python33\Lib) 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.

    0 讨论(0)
  • 2020-11-21 18:36

    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')
    
    0 讨论(0)
  • 2020-11-21 18:36

    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)

    0 讨论(0)
  • 2020-11-21 18:37

    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.

    0 讨论(0)
  • 2020-11-21 18:37

    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
    
    0 讨论(0)
  • 2020-11-21 18:38

    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()
    
    0 讨论(0)
提交回复
热议问题