在很多时候,如果我们在控制台中使用 Python, 随着时间的推移,可能会发现屏幕越来越乱。
如下图,我们跑了不少的测试程序,在屏幕上有很多的输出。
在 Windows 中,我们会使用 cls 命令清屏。
在 Python,应该怎么样才能清屏呢?
其实 Python 并没有清屏幕的命令,也没有内置内置命令可以用。
但是,我们可以使用快捷键:
ctrl+l
来进行清屏。
当然,如果你希望使用一个自定义函数的方法来进行清屏。
# -*- coding: utf-8 -*-
# import only system from os
from os import system, name
# import sleep to show output for some time period
from time import sleep
# define our clear function
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
# print out some text
print('Hello CWIKIUS\n' * 10)
# sleep for 2 seconds after printing output
sleep(2)
# now call function we defined above
clear()
如上面使用的代码,我们在运行后,将会看到屏幕在退出前被清理了。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有