如何在运行 python 脚本时清除 cmd/终端 [英] How to clear cmd/terminal while running a python script

查看:45
本文介绍了如何在运行 python 脚本时清除 cmd/终端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直看到在运行脚本时清除外壳的方法,但是有没有办法在 CMD 中运行脚本时清除屏幕?我目前的方法是这样工作的:

I keep seeing ways to clear the shell while running a script, however is there a way to clear the screen while running a script in the CMD? My current method works like this:

clear.py

import title
def clear():
    print('\n' * 25)
    title.title()

游戏.py

from engine import clear
clear.clear()
print(Fore.CYAN + Style.BRIGHT + "--------------------------------")

但是这种方法并不是很可靠,因为某些 cmd 大小在所有计算机上都不同,我也没有在 OSx 上尝试过.

However this method isn't really reliable as some cmd sizes are different on all computers, nor have I tried it on OSx.

推荐答案

这是另一种处理 Windows 和类 Unix 系统(Linux、OSX 等)的方法:

Here's another way, that handles Windows cases as well as Unix-like systems (Linux, OSX, etc.):

import os
os.system('cls' if os.name == 'nt' else 'clear')

clear 命令适用于所有 Unix- 类似系统(即 OSX、Linux 等).Windows 等效项是 cls.

这篇关于如何在运行 python 脚本时清除 cmd/终端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆