是否存在任何标准的“附带电池”方法来清除Python脚本中的终端屏幕,或者我必须去诅咒(库,而不是单词)?


当前回答

前段时间偶然发现的

def clearscreen(numlines=100):
  """Clear the console.
numlines is an optional argument used only as a fall-back.
"""
# Thanks to Steven D'Aprano, http://www.velocityreviews.com/forums

  if os.name == "posix":
    # Unix/Linux/MacOS/BSD/etc
    os.system('clear')
  elif os.name in ("nt", "dos", "ce"):
    # DOS/Windows
    os.system('CLS')
  else:
    # Fallback for other operating systems.
    print('\n' * numlines)

然后使用clearscreen()

其他回答

这适用于所有平台,并且在Python 2和3中都有效。

def clear(number):
    for i in range(number):
        print(" ")

然后输入clear(numberhere)来清除。

为什么没有人谈论过在Windows中使用Ctrl+L或者在Mac中使用Cmd+L。 这无疑是清除屏幕的最简单方法。

您可以对terminfo数据库进行分解,但这样做的函数无论如何都是在curses中。

这将清除25个新行:

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

clear()

我在pydev中使用eclipse。我更喜欢换行解决方案比范围内的for num。for循环抛出警告,而打印换行符则不会。 如果您想在clear语句中指定换行数,请尝试此变体。

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

clear(25)

你可以自己做。这将不依赖于你的终端,或操作系统类型。

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

clear(80)
print "hello"