Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 请清除屏幕_Python_Macos_Screenshot - Fatal编程技术网

Python 请清除屏幕

Python 请清除屏幕,python,macos,screenshot,Python,Macos,Screenshot,我仍在尝试进行测验,但我想为我的问题输入一个清晰的屏幕代码。所以,在研究之后,我发现了一个有效的代码,但是它把我的问题放到了屏幕的底部。这是我拍的一张截图: 下面是我找到的清除屏幕代码的示例: print "\n" * 40 所以我试着把“40”改成“20”,但没有效果。我在Mac上操作,所以 import os os.system('blahblah') 不起作用。请帮忙 如@pNre所述,向您展示了如何使用ASCI逃逸序列执行此操作 如果要使用os模块执行此操作,则在Mac上,命令为c

我仍在尝试进行测验,但我想为我的问题输入一个清晰的屏幕代码。所以,在研究之后,我发现了一个有效的代码,但是它把我的问题放到了屏幕的底部。这是我拍的一张截图:

下面是我找到的清除屏幕代码的示例:

print "\n" * 40
所以我试着把“40”改成“20”,但没有效果。我在Mac上操作,所以

import os
os.system('blahblah')
不起作用。请帮忙

如@pNre所述,向您展示了如何使用ASCI逃逸序列执行此操作

如果要使用
os
模块执行此操作,则在Mac上,命令为
clear

import os
os.system('clear')
注意:您在Mac上并不意味着操作系统('blahblah')将无法工作。您传递给操作系统的命令更可能是错误的


有关如何在Windows/Linux上执行此操作,请参阅下面的答案。

可以通过子进程完成

import subprocess as sp

clear_the_screen = sp.call('cls', shell=True) # Windows <br>
clear_the_screen = sp.call('clear', shell=True) # Linux
将子流程作为sp导入
清除屏幕=sp.call('cls',shell=True)#窗口
清除屏幕=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中的“达尔文”。
subprocess.call()
执行系统调用。例如,
子流程调用(['ls','-l'])

可能重复的