是否存在任何标准的“附带电池”方法来清除Python脚本中的终端屏幕,或者我必须去诅咒(库,而不是单词)?
当前回答
您可以尝试使用clear,但它可能并非在所有Linux发行版上都可用。在windows上使用你提到的cls。
import subprocess
import platform
def clear():
subprocess.Popen( "cls" if platform.system() == "Windows" else "clear", shell=True)
clear()
注意:控制终端屏幕可能被认为是一种糟糕的形式。你在考虑使用期权吗?让用户自己决定是否要清除屏幕可能会更好。
其他回答
清除屏幕的方法可能有些俗气,但在我所知的任何平台上都适用,如下:
for i in xrange(0,100):
print ""
所以我想在这里发表我的意见…
似乎没有人对这个问题提供一个真实的答案,每个人的回答都是“不使用os.system()这是邪恶的!!”没有解释,也没有提供依赖于打印新行的解决方案。
对于那些需要清除终端屏幕并向后滚动的用户,无论出于何种原因,您都可以使用以下代码:
import os
def clear():
'''
Clears the terminal screen and scroll back to present
the user with a nice clean, new screen. Useful for managing
menu screens in terminal applications.
'''
os.system('cls' if os.name == 'nt' else 'echo -e \\\\033c')
print('A bunch of garbage so we can garble up the screen...')
clear()
# Same effect, less characters...
def clear():
'''
Clears the terminal screen and scroll back to present
the user with a nice clean, new screen. Useful for managing
menu screens in terminal applications.
'''
os.system('cls||echo -e \\\\033c')
这达到了OP所期望的效果。它确实使用os.system()命令,所以如果这是邪恶的,有人知道使用subprocess.call()实现这一点的方法,请评论,因为我也更喜欢使用subprocess,但根本不熟悉它。
从操作系统导入系统;系统(清晰的)”
对于Windows, Mac和Linux,您可以使用以下代码:
import subprocess, platform
if platform.system()=="Windows":
if platform.release() in {"10", "11"}:
subprocess.run("", shell=True) #Needed to fix a bug regarding Windows 10; not sure about Windows 11
print("\033c", end="")
else:
subprocess.run(["cls"])
else: #Linux and Mac
print("\033c", end="")
jamesnotjim为Mac测试了打印(“\033c”,end=“”),我在Linux和Windows上测试了它(它不适用于Windows,因此调用cls的其他代码)。我不记得我第一次看到使用print("\033c")和/或printf版本:subprocess.run("printf '\033c'", shell=True)。
Rolika指出,end=""将阻止它随后打印新行。
注意,新版本的Ubuntu会很好地清除屏幕(不只是向下滚动,让它看起来是清除的),不像旧版本。
注意,用ESC c ("\033c")重置终端将使光标下划线并闪烁。如果你不想这样,你可以使用这些代码将其更改为另一种风格(在GNOME Terminal 3.44.0上测试,使用VTE 0.68 +BIDI +GNUTLS +ICU +SYSTEMD):
下划线闪烁:"\033[0 q" 块闪烁:"\033[1 q" 方块:"\033[2 q" 下划线闪烁:"\033[3 q" 下划线:"\033[4 q" 细条闪烁:“\033[5 q” 细条:"\033[6q "(大于6的数字似乎也是这样)
还要注意,你可以在Linux上做以下任何事情来清除屏幕:
print("\033c", end=""): print("\u001bc", end="") print("\U0000001bc", end="") print("\x1bc", end="") subprocess.run(["clear"]) #This doesn't reset the whole terminal subprocess.run('echo -ne "\033c"', shell=True) subprocess.run('echo -ne "\ec"', shell=True) subprocess.run('echo -ne "\u001bc"', shell=True) subprocess.run('echo -ne "\U0000001bc"', shell=True) subprocess.run('echo -ne "\x1bc"', shell=True) subprocess.run("printf '\033c'", shell=True) subprocess.run("printf '\ec'", shell=True) subprocess.run("printf '\u001bc'", shell=True) subprocess.run("printf '\U0000001bc'", shell=True) subprocess.run("printf '\x1bc'", shell=True)
我相信下面的代码应该是为了清除你必须向上滚动才能看到的内容(但它很难与另一个命令一起使用而没有问题):
打印('*)
这可以做和clear以前做的一样的事情(所以你可以向上滚动查看被删除的内容,除了它不会将光标上升到顶部):
打印('**)
这将清除25个新行:
def clear():
print(' \n' * 25)
clear()
我在pydev中使用eclipse。我更喜欢换行解决方案比范围内的for num。for循环抛出警告,而打印换行符则不会。 如果您想在clear语句中指定换行数,请尝试此变体。
def clear(j):
print(' \n' * j)
clear(25)
推荐文章
- Numpy Max vs amax vs maximum
- 我应该在.gitignore文件中添加Django迁移文件吗?
- 每n行有熊猫
- 实例属性attribute_name定义在__init__之外
- 如何获取在Python中捕获的异常的名称?
- 第一次出现的值大于现有值的Numpy
- 如何从Python函数中返回两个值?
- 前一个月的Python日期
- Python中方括号括起来的列表和圆括号括起来的列表有什么区别?
- Python日志记录不输出任何东西
- 每n秒运行特定代码
- SQLAlchemy是否有与Django的get_or_create等价的函数?
- 如何将python datetime转换为字符串,具有可读格式的日期?
- 美丽的汤和提取div及其内容的ID
- 在Python中重置生成器对象