查看: 14  |  回复: 0
  Python 的清屏命令,调用清屏函数,清除控制台屏幕 子程序 clear_screen()
楼主
发表于 2025年4月22日 01:15
# 导入 os 模块
import os

try:
    print('Hello')

    # 定义清屏函数,根据操作系统类型执行不同的清屏命令
    def clear_screen():
        # 如果操作系统是 Windows,执行 'cls' 命令清屏;否则执行 'clear' 命令
        os.system('cls' if os.name == 'nt' else 'clear')

    # 调用清屏函数,清除控制台屏幕
    clear_screen()

    print('World')
except Exception as e:
    print(f"程序执行过程中出现错误: {e}")
finally:
    input("Press Enter to exit...")


您需要登录后才可以回帖 登录 | 立即注册
【本版规则】请勿发表违反国家法律的内容,否则会被冻结账号和删贴。
用户名: 立即注册
密码:
2020-2025 MaNongKu.com