如何在 IDLE 中清除 Python Shell
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17455105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to clear Python Shell in IDLE
提问by José
I'm running a script that uses a module that prints a lot of things on screen and I'm running out of RAM.
I can't make the module not to write anything for now.
The Python Shell stores absolutely everything and I want to clear it.
On similar questions the only answer I could find was to write os.system('cls')
(on Windows), but it doesn't delete anything.
Is there a way to clear the Python Shell or to limit its size?
Thanks
我正在运行一个脚本,该脚本使用一个在屏幕上打印很多内容的模块,但内存不足。
我现在不能让模块不写任何东西。
Python Shell 存储了所有内容,我想清除它。
在类似的问题上,我能找到的唯一答案是写os.system('cls')
(在 Windows 上),但它不会删除任何内容。
有没有办法清除 Python Shell 或限制其大小?
谢谢
Edit.
Well, this question was marked as duplicate and I am asked to clarify why it isn't.
I state that os.system('cls')
doesn't work, while the answer to the question I supoosedly duplicate is to write os.system('cls')
.
编辑。
好吧,这个问题被标记为重复,我被要求澄清为什么不是。
我声明这os.system('cls')
不起作用,而我应该重复的问题的答案是写os.system('cls')
.
采纳答案by Mongoose1021
By python shell, do you mean IDLE? Some quick googling suggests that IDLE doesn't have a clear screen even though lots of people seem to want one. If it's in a shell, then I'm surprised 'cls' isn't working.
通过python shell,您的意思是IDLE吗?一些快速的谷歌搜索表明 IDLE 没有清晰的屏幕,即使很多人似乎想要一个。如果它在 shell 中,那么我很惊讶 'cls' 不起作用。
If you like working in Idle, you might look at this for getting the functionality you want: http://idlex.sourceforge.net/extensions.html#ShellEnhancementsThe internet seems to think you should just stop using IDLE, however.
如果你喜欢在 Idle 中工作,你可以看看这个以获得你想要的功能:http: //idlex.sourceforge.net/extensions.html#ShellEnhancements然而,互联网似乎认为你应该停止使用 IDLE。
回答by Jordan
How are you running the script? Are you calling it from a shell? If so, you can redirect all output to a file like this:
你是如何运行脚本的?你是从 shell 调用它吗?如果是这样,您可以将所有输出重定向到这样的文件:
python my_script.py > /out/to/some/path.log
回答by nmichaels
If the module is just printing, you can use this:
如果模块只是打印,你可以使用这个:
import sys
sys.stdout = open('/dev/null', 'a') # or a real file, if you care about output.