首页 > 编程语言 > 详细

如何在 Python 中清屏

时间:2021-03-09 09:23:53      阅读:27      评论:0      收藏:0      [点我收藏+]

在很多时候,如果我们在控制台中使用 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()

如上面使用的代码,我们在运行后,将会看到屏幕在退出前被清理了。

https://www.ossez.com/t/python/13375

如何在 Python 中清屏

原文:https://www.cnblogs.com/huyuchengus/p/14503113.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!