我还在尝试做我的测验,但我想为我的问题输入一个清除屏幕的代码。所以,经过研究,我找到了一个工作的代码,但它把我的问题的方式到屏幕的底部。这是我拍的一张截图。
这是我找到的一个清除屏幕代码的例子。
print "\n" * 40
我试着把 "40 "改成 "20",但没有效果 我在Mac上操作,所以
import os
os.system('blahblah')
不工作。请大家帮忙!
正如@pNre指出的那样。这个问题 向你展示了如何使用ASCI转义序列来实现这一目的。
如果你想使用 os
模块,那么在Mac上的命令是 clear
.
import os
os.system('clear')
注意:您使用的是Mac机,这并不意味着 os.system('blahblah')
将无法工作。更有可能是你传递给 os.system
是错误的。
如何在WindowsLinux上进行,请看下面的答案。
可以通过子进程
import subprocess as sp
clear_the_screen = sp.call('cls', shell=True) # Windows <br>
clear_the_screen = sp.call('clear', shell=True) # Linux
该功能可在任何操作系统(Unix、Linux、OS X和Windows)中使用。 Python 2和Python 3
from platform import system as system_name # Returns the system/OS name
from subprocess import call as system_call # Execute a shell command
def clear_screen():
"""
Clears the terminal screen.
"""
# Clear screen command as function of OS
command = 'cls' if system_name().lower()=='windows' else 'clear'
# Action
system_call([command])
在Windows中,命令是 cls
在类unix系统中,命令是 clear
. platform.system()
返回平台名称。例:在macOS中。'Darwin'
在macOS中。subprocess.call()
执行系统调用。例:在macOS中,.返回一个系统调用。subprocess.call(['ls','-l'])