如何清除解释器控制台?

本文翻译自: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: 特征:

  1. You can copy-paste the code into your shell or script. 您可以代码复制粘贴到您的shell或脚本中。
  2. You can use it as you like: 您可以根据需要使用它:

     >>> clear() >>> -clear >>> clear # <- but this will only work on a shell 
  3. You can import it as a module: 您可以其作为模块导入

     >>> from clear import clear >>> -clear 
  4. You can call it as a script: 您可以其称为脚本:

     $ python clear.py 
  5. It is truly multiplatform ; 它是真正的多平台 ; if it can't recognize your system 如果无法识别您的系统
    ( ce , nt , dos or posix ) it will fall back to printing blank lines. centdosposix ),它将退回到打印空白行。


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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值