pok*_*oke 280
一个简单的跨平台解决方案是cls在Windows或clearUnix系统上使用该命令.与之配合使用os.system,这是一个不错的单线程:
import os
os.system('cls' if os.name == 'nt' else 'clear')
Run Code Online (Sandbox Code Playgroud)
Jor*_*ril 106
逃脱序列怎么样?
print(chr(27) + "[2J")
Run Code Online (Sandbox Code Playgroud)
sar*_*eph 76
为什么没有任何人谈过只是简单地做Ctrl+ L在Windows或 Cmd+ L在Mac上.当然是清除屏幕最简单的方法.
Dav*_*rby 35
如果您使用的是Linux/UNIX系统,则打印ANSI转义序列以清除屏幕应该可以完成工作.您还需要将光标移动到屏幕顶部.这适用于任何支持ANSI的终端.
import sys
sys.stderr.write("\x1b[2J\x1b[H")
Run Code Online (Sandbox Code Playgroud)
除非已启用ANSI支持,否则这将无法在Windows上运行.Windows可能有一个等效的控制序列,但我不知道.
Shu*_*ule 28
对于Windows,Mac和Linux,你可以使用下面的代码:
import subprocess, platform
if platform.system()=="Windows":
subprocess.Popen("cls", shell=True).communicate() #I like to use this instead of subprocess.call since for multi-word commands you can just type it out, granted this is just cls and subprocess.call should work fine
else: #Linux and Mac
print("\033c", end="")
Run Code Online (Sandbox Code Playgroud)
jamesnotjim测试print("\033c", end="")了Mac,我在Linux和Windows上测试过它(它不适用于Windows,因此调用其他代码cls).我不记得是谁,这是我第一次看到用印刷物("\ 033C")和/或printf的版本:subprocess.Popen("printf '\033c'", shell=True).communicate().
rolika指出,以后end=""会阻止它打印新行.
Vas*_*sin 23
至于我,最优雅的变种:
import os
os.system('cls||clear')
Run Code Online (Sandbox Code Playgroud)
您可以尝试依赖clear,但可能并非在所有Linux发行版上都可用.在Windows上使用你提到的cls.
import subprocess
import platform
def clear():
subprocess.Popen( "cls" if platform.system() == "Windows" else "clear", shell=True)
clear()
Run Code Online (Sandbox Code Playgroud)
注意:控制终端屏幕可能被视为不良形式.你在考虑使用一个选项吗?让用户决定是否要清除屏幕可能会更好.
这将适用于 Python2 或 Python3 版本
print (u"{}[2J{}[;H".format(chr(27), chr(27)))
Run Code Online (Sandbox Code Playgroud)
纯Python解决方案.
不依赖于ANSI或外部命令.
只有您的终端必须能够告诉您视线中有多少行.
from shutil import get_terminal_size
print("\n" * get_terminal_size().lines, end='')
Run Code Online (Sandbox Code Playgroud)
Python版本> = 3.3.0
所以以为我会把我的两分钱丢在这里...
似乎没有人提供OP问题的真实答案,每个人都回答“不要使用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')
Run Code Online (Sandbox Code Playgroud)
这具有OP的预期效果。它确实使用了os.system()命令,所以如果这很邪恶,并且有人知道使用subprocess.call()实现此方法的方法,请评论,因为我也更喜欢使用subprocess,但一点都不熟悉。
小智 5
前段时间遇到过这个问题
def clearscreen(numlines=100):
"""Clear the console.
numlines is an optional argument used only as a fall-back.
"""
# Thanks to Steven D'Aprano, http://www.velocityreviews.com/forums
if os.name == "posix":
# Unix/Linux/MacOS/BSD/etc
os.system('clear')
elif os.name in ("nt", "dos", "ce"):
# DOS/Windows
os.system('CLS')
else:
# Fallback for other operating systems.
print('\n' * numlines)
Run Code Online (Sandbox Code Playgroud)
然后只使用clearscreen()
只需使用:
print("\033c")
Run Code Online (Sandbox Code Playgroud)
这将清除终端窗口。
| 归档时间: |
|
| 查看次数: |
308767 次 |
| 最近记录: |