本文翻译自:How to clear the interpreter console?
Like most Python developers, I typically keep a console window open with the Python interpreter running to test commands, dir()
stuff, help() stuff
, etc. 像大多数Python开发人员一样,我通常会打开控制台窗口,并运行Python解释器来测试命令, dir()
东西, help() stuff
等。
Like any console, after a while the visible backlog of past commands and prints gets to be cluttered, and sometimes confusing when re-running the same command several times. 像任何控制台一样,过一会儿,过去的命令和打印的可见积压会变得混乱,有时在重新运行同一命令几次时会造成混乱。 I'm wondering if, and how, to clear the Python interpreter console. 我想知道是否以及如何清除Python解释器控制台。
I've heard about doing a system call and either calling cls
on Windows or clear
on Linux, but I was hoping there was something I could command the interpreter itself to do. 我听说过要进行系统调用,或者在Windows上调用cls
,或者在Linux上clear
,但是我希望可以命令解释器自己执行一些操作。
Note: I'm running on Windows, so Ctrl+L
doesn't work. 注意:我在Windows上运行,所以Ctrl+L
不起作用。
#1楼
参考:https://stackoom.com/question/2AkM/如何清除解释器控制台
#2楼
Here's the definitive solution that merges all other answers . 这是合并所有其他答案 的最终解决方案 。 Features: 特征:
- You can copy-paste the code into your shell or script. 您可以将代码复制粘贴到您的shell或脚本中。
You can use it as you like: 您可以根据需要使用它:
>>> clear() >>> -clear >>> clear # <- but this will only work on a shell
You can import it as a module: 您可以将其作为模块导入 :
>>> from clear import clear >>> -clear
You can call it as a script: 您可以将其称为脚本:
$ python clear.py
It is truly multiplatform ; 它是真正的多平台 ; if it can't recognize your system 如果无法识别您的系统
(ce
,nt
,dos
orposix
) it will fall back to printing blank lines. (ce
,nt
,dos
或posix
),它将退回到打印空白行。
You can download the [full] file here: https://gist.github.com/3130325 您可以在此处下载[full]文件: https : //gist.github.com/3130325
Or if you are just looking for the code: 或者,如果您只是在寻找代码:
class clear:
def __call__(self):
import os
if os.name==('ce','nt','dos'): os.system('cls')
elif os.name=='posix': os.system('clear')
else: print('\n'*120)
def __neg__(self): self()
def __repr__(self):
self();return ''
clear=clear()
#3楼
I'm new to python (really really new) and in one of the books I'm reading to get acquainted with the language they teach how to create this little function to clear the console of the visible backlog and past commands and prints: 我是python的新手(真的很新),在我读的一本书中,他们熟悉该语言,他们教他们如何创建此小功能来清除控制台的可见积压以及过去的命令和打印内容:
Open shell / Create new document / Create function as follows: 打开shell /创建新文档/创建函数,如下所示:
def clear():
print('\n' * 50)
Save it inside the lib folder in you python directory (mine is C:\\Python33\\Lib) Next time you nedd to clear your console just call the function with: 将其保存在python目录的lib文件夹中(我的文件夹为C:\\ Python33 \\ Lib)下次您需要清除控制台时,只需使用以下函数调用该函数:
clear()
that's it. 而已。 PS: you can name you function anyway you want. PS:您可以随意命名自己的功能。 Iv' seen people using "wiper" "wipe" and variations. 我见过人们使用“雨刮器”,“擦拭”和其他形式。
#4楼
OK, so this is a much less technical answer, but I'm using the Python plugin for Notepad++ and it turns out you can just clear the console manually by right-clicking on it and clicking "clear". 好的,所以这是一个技术性较差的答案,但是我使用的是Notepad ++的Python插件,事实证明,您可以通过右键单击控制台并单击“清除”来手动清除控制台。 Hope this helps someone out there! 希望这可以帮助某人!
#5楼
我发现最简单的方法就是关闭窗口并运行模块/脚本以重新打开外壳。
#6楼
只是用这个..
print '\\n'*1000