在Python中清除之前的终端输出

-1 投票
1 回答
1704 浏览
提问于 2025-04-18 01:33

有没有办法清除终端之前的输出?如果有的话,怎么做呢?下面有个代码示例可以参考。

print "Hello..."
#Code that clears previous terminal output
print "All cleared!"

我知道在移动应用pythonista中有一个iOS模块可以做到这一点,但我想知道在Linux上怎么做。

1 个回答

2

根据你使用的是Windows还是Linux,你可以这样做:

import os
os.system('cls')   #Windows
os.system('clear') #Linux

举个例子:

print 'Hello...'
os.system('clear') #or os.system('cls') for Windows
print "All cleared!"

撰写回答