如何在Python中清除屏幕? [重复]

问题描述 投票:0回答:2

这个问题在这里已有答案:

我在Python 3.7.1上运行,我一直在试图找到一种方法来清除任何以前打印过的消息的屏幕。问题是os.system("cls")什么都不做,它只会让一个小窗口弹出一小段时间,然后关闭。我试图在最后添加一个\ n并将其乘以有多少个字母,仍然不起作用。

python screen clear
2个回答
0
投票

不幸的是,没有内置的关键字或功能/方法来清除屏幕。所以,我们自己做。

我们可以使用ANSI转义序列,但这些不可移植,可能无法产生所需的输出。

# import only system from os 
from os import system, name 

# import sleep to show output for some time period 
from time import sleep 

# define our clear function 
def clear(): 

    # for windows 
    if name == 'nt': 
        _ = system('cls') 

    # for mac and linux(here, os.name is 'posix') 
    else: 
        _ = system('clear') 

# print out some text 
print('hello geeks\n'*10) 

# sleep for 2 seconds after printing output 
sleep(2) 

# now call function we defined above 
clear() 

-1
投票

我不认为有办法,或者至少从未见过。但是,有一种看起来像的解决方法

print "\n" * 100

这将只打印100个换行符,这将为您清除屏幕。

你也可以把它放在一个函数中

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

然后当你需要它时,只需用cls()调用它

© www.soinside.com 2019 - 2024. All rights reserved.