【问题标题】:Fully clear terminal on Mac OS X without exiting Python script在不退出 Python 脚本的情况下完全清除 Mac OS X 上的终端
【发布时间】:2020-03-31 01:01:17
【问题描述】:

我正在使用 Python3 运行一个简单的实用程序,该实用程序接受由逗号、制表符或管道分隔的文本输入(通常从 csv 复制)并在“|”周围加入文本以提高可读性。

提交空输入后,终端应清除为下一个输入做好准备。

这在 Fedora 31 上完美运行,但在 OS X 上,终端只是向下移动以隐藏其余文本,而不是实际清除它。我尝试过使用“clear”、“tput reset”,但不知道为什么这不起作用。如果我在 Perl 中使用 system("tput reset") 而不是 Python,它会正确清除 OS X 上的终端。

from os import system
import re

def clearScreen():
    system('tput reset')


clearScreen()

while True:
    block = []
    line = '_'

    while line:
        line = input('')
        if len(line) > 0:
            block.append(line)
        else:
            clearScreen()

    for row in block:
        print(' | '.join(re.split('[|,\t]', row)) + '\n')

【问题讨论】:

    标签: python python-3.x macos terminal


    【解决方案1】:

    这是 Mac 的一个怪癖。

    改用system("printf '\\33c\\e[3J'") 就可以了。

    【讨论】:

    • 完美!谢谢
    猜你喜欢
    • 2022-06-11
    • 2011-01-12
    • 2022-11-12
    • 1970-01-01
    • 1970-01-01
    • 2010-10-29
    • 2014-12-10
    • 2011-11-13
    • 2014-07-15
    相关资源
    最近更新 更多