Clear terminal in Python

2018-12-31 09:03发布

Does any standard "comes with batteries" method exist to clear the terminal screen from a Python script, or do I have to go curses (the libraries, not the words)?

27条回答
与风俱净
2楼-- · 2018-12-31 09:27

This will clear 25 new lines:

def clear():
    print(' \n' * 25)

clear()

I use eclipse with pydev. I like the newline solution better than the for num in range . The for loop throws warnings, while the print newline doesn't. If you want to specify the number of newlines in the clear statement try this variation.

def clear(j):
    print(' \n' * j)

clear(25)
查看更多
回忆,回不去的记忆
3楼-- · 2018-12-31 09:30

If you are on a Linux/UNIX system then printing the ANSI escape sequence to clear the screen should do the job. You will also want to move cursor to the top of the screen. This will work on any terminal that supports ANSI.

import sys
sys.stderr.write("\x1b[2J\x1b[H")

This will not work on Windows unless ANSI support has been enabled. There may be an equivalent control sequence for Windows, but I do not know.

查看更多
冷夜・残月
4楼-- · 2018-12-31 09:32

You can use call() function to execute terminal's commands :

from subprocess import call
call("clear")
查看更多
流年柔荑漫光年
5楼-- · 2018-12-31 09:33

you can make your own. this will not be dependent on your terminal, or OS type.

def clear(num):
    for i in range(num): print 

clear(80)
print "hello"
查看更多
伤终究还是伤i
6楼-- · 2018-12-31 09:36

I would do it in this way to make it look more like bash:

Just create a file named .pythonstartup at Home directory and use poke's answer in a function

On Linux:

echo "from subprocess import call
def clear(int=None):  
    call('clear')
    if int == 0:
       exit()
clear()" >> $HOME/.pythonstartup ; export PYTHONSTARTUP=$HOME/.pythonstartup ; python

You can add export PYTHONSTARTUP=$HOME/.pythonstartup to your ./bashrc file

Since what I care about is space; a call to the function will not display the python interpreter description at startup, but you can remove clear() to retain it.

Using it like a normal function should do the trick without printing the exit status:

>>> clear()

If you pass the argument 0 to the function it will clear the screen and exit successfully so you can continue using the shell in a clean screen

>>> clear(0)
查看更多
千与千寻千般痛.
7楼-- · 2018-12-31 09:37

So just thought I would throw my two cents in here...

No one has provided a true answer to OP question it seems, everyone either responds with 'NO DONT USE os.system() it's evil!!!' without explanation or provides a solution that relies on printing new lines.

For those that need to clear the terminal screen and scroll back, for whatever reason, you can use the following code:

import os

def clear():
    '''
    Clears the terminal screen and scroll back to present
    the user with a nice clean, new screen. Useful for managing
    menu screens in terminal applications.
    '''
    os.system('cls' if os.name == 'nt' else 'echo -e \\\\033c')

print('A bunch of garbage so we can garble up the screen...')
clear()

# Same effect, less characters...

def clear():
    '''
    Clears the terminal screen and scroll back to present
    the user with a nice clean, new screen. Useful for managing
    menu screens in terminal applications.
    '''
    os.system('cls||echo -e \\\\033c')

This has the OP's desired effect. It does use the os.system() command so if that's evil and someone knows a way of implementing this using subprocess.call() please comment as I would also prefer to use subprocess but am not familiar with it at all.

查看更多
登录 后发表回答