linux - 跨平台控制台清算?

标签 linux python-3.x windows

<分区>

如果我想在没有操作系统限制的情况下清除控制台,我需要做什么? 我知道在 Linux 和 Mac 上存在命令“clear”,而 Windows 有“cls”。 我想时不时地在三大系统上清除控制台,而不是亲自选择“clear”或“cls”。

到目前为止我的想法是

import platform
os = platform.system()
if os == "Windows":
    clear = 'cls'
else:
    clear = 'clear'

然后根据操作系统使用 clear 作为变量,但它不起作用。这样的事情真的可能吗?

最佳答案

为确保准确性,请使用 sys 并作为良好做法使用 subprocess

#usr/bin/env python

from sys import platform
from subprocess import run

command = {'win32': 'cls', 'linux': 'clear'}

if __name__ == '__main__':
     run(command[platform], shell=True)

正如 BartoszKP 所说,有很多方法可以做到这一点,我发现这是一种非常简洁的方法。

关于linux - 跨平台控制台清算?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372963/

相关文章:

linux - tmpfs 在嵌入式 Linux 系统上的使用

linux - 将代码从 Microsoft Visual C++ 迁移到 debian (raspberry)

linux - 使用 openssl 1.0.x 构建 curl,ldd 显示对 0.9.8 和 1.0.x 的依赖

python - 我怎样才能修复这个汉诺塔程序以获得我想要的输出?

python - 在方法开头组合检查

Python 3 : Getting TypeError: Slices must be integers. .. 但我相信它们

linux - Bluetoothctl 无需任何用户交互

windows - RStudio 在 Windows 10 上未检测到 .gitconfig 文件

windows - CMake 将 unix 转换为 windows 路径

python - 如何在 python 中修改现有文件的压缩 itxt 记录?