2014-04-05 610 views
2

我仍在嘗試進行測驗,但我想爲我的問題輸入清晰的屏幕代碼。所以,在研究之後,我發現了一個可行的代碼,但它將我的問題帶到了屏幕的底部。下面是截圖我帶:Python清除屏幕代碼?

The Image is below

這裏是清屏的代碼,我發現了一個例子:

print "\n" * 40 

所以我試圖改變「40」至「20」,但沒有影響。我在Mac上操作,因此

import os 
os.system('blahblah') 

不起作用。請幫忙!

+1

可能重複[在Python中清除終端](http://stackoverflow.com/questions/2084508/clear-terminal-in-python) – pNre

回答

2

As @pNre指出,this question顯示瞭如何使用ASCI轉義序列執行此操作。

如果您想使用os模塊執行此操作,則在Mac上,命令爲clear

import os 
os.system('clear') 

注意:您在Mac上的事實並不意味着os.system('blahblah')將不起作用。這很可能是你傳遞給os.system的命令是錯誤的。

請參閱下面的答案,瞭解如何在Windows/Linux上執行此操作。

2

進口作爲子SP

clear_the_screen = sp.call( 'CLS',殼=真)#的Windows
clear_the_screen = sp.call( '清',殼=真)#Linux的

0

此功能在任何操作系統工程(UNIX,Linux和OS X和Windows)
的Python 2和Python 3

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()返回平臺名稱。防爆。 'Darwin'在macOS中。
subprocess.call()執行系統調用。防爆。 subprocess.call(['ls','-l'])