[英]Completely clearing the terminal window using Python
How can I in Python (v 2.7.2) completely clear the terminal window?如何在 Python (v 2.7.2) 中完全清除终端窗口?
This doesn't work since it just adds a bunch of new lines and the user can still scroll up and see earlier commands这不起作用,因为它只是添加了一堆新行,用户仍然可以向上滚动并查看之前的命令
import os
os.system('clear')
I've tried this and it works:我试过这个,它的工作原理:
>>> import os
>>> clear = lambda : os.system('clear')
>>> clear()
clear()
clear()
On Windows:在 Windows 上:
import os
os.system('cls')
On Linux:在 Linux 上:
import os
os.system('clear')
for windows:对于窗户:
import os
def clear(): os.system('cls')
for linux:对于Linux:
import os
def clear(): os.system('clear')
then to clear the terminal simply call:然后清除终端只需调用:
clear()
No need of importing any libraries.无需导入任何库。 Simply call the cls function.
只需调用 cls 函数。
cls()
I am using Linux and I found it much easier to just use我正在使用 Linux,我发现它更容易使用
import os
in the header of the code and在代码的标题和
os.system('clear')
in the middle of my script.在我的脚本中间。 No lambda or whatever was needed to get it done.
不需要 lambda 或任何东西来完成它。 What I guess is important to pay attention to, is to have the
import os
in the header of the code, the very beginning where all other import
commands are being written...我想重要的是要注意,在代码的标题中有
import os
,这是所有其他import
命令被写入的开始......
Adding for completeness, if you want to do this without launching an external command, do:添加完整性,如果您想在不启动外部命令的情况下执行此操作,请执行以下操作:
print('\x1bc')
Only tested on Linux.仅在 Linux 上测试。
You can not control the users terminal.你不能控制用户终端。 That's just how it works.
这就是它的工作原理。
Think of it like write only.把它想象成只写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.