如何在 python 中清除屏幕?
在 Python 中我们有时会链接输出,而我们想清除单元格提示符的屏幕,我们可以通过按 Control + l 来清除屏幕。但有时候我们需要根据程序的输出量和我们希望如何格式化输出,以编程方式清除屏幕。在这种情况下,我们需要在 Python 脚本中放置一些命令,以便在程序需要时清除屏幕。
我们需要从 Python 的 OS 模块中获取 system() 来清除屏幕。对于不同平台(如 Windows 和 Linux),我们需要传递不同的命令,如以下示例所示。我们还使用“_”变量,用于保存解释器中最后表达式的值。
示例
import os
from time import sleep
# The screen clear function
def screen_clear():
# for mac and linux(here, os.name is 'posix')
if os.name == 'posix':
_ = os.system('clear')
else:
# for windows platfrom
_ = os.system('cls')
# print out some text
print("The platform is: ", os.name)
print("big output\n"* 5)
# wait for 5 seconds to clear screen
sleep(5)
# now call function we defined above
screen_clear()输出
运行以上代码会给出以下结果 −
The platform is: nt big output big output big output big output big output
在结果窗口中的 5 秒后,上述输出会被清除。
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP