How to remove lines from stdout in Python - in SciTe, Idle, Eclipse or other editor with console

删除回忆录丶 提交于 2019-12-20 04:38:42

问题


For a standard Python Console window, you can remove the last line with for example sys.stdout.write('\r'+' '*len(line)) as I explained here.

But for editors like SciTe, Idle or Eclipse (with PyDev), the stdout is a file-type object, probably flushing its content to the console window leaving its buffer empty and thus not allowing modify the content once it has appeared on in the window. What trick can be applied to, for example, erase the last line of such console? Is there a one-size-fits-all way to do it, generating the same result if the script is run outside of the editor writing to a standard Python Console Window?

edit: Checking type(os.sys.stdout) shows:

  • SciTe: <type 'file'>
  • Idle: <class 'idlelib.rpc.RPCProxy'>
  • Eclipse: <type 'file'>
  • Python Console Window: <type 'file'>

In the standard python console stdout, the result stdout.write('\r') of is different from the consoles of SciTe and Eclipse, thus the type of the stdout does not define its implementation.

edit: just saw java folks having similar problem and reported is as a bug...


回答1:


I am comming to the conclusion that it is not possible, please correct me if I am wrong.

Python Console Window (terminal) does not 'flush' the written line until the next line is passed, as if it keeps a carriage returns like '\n' in memory that can be ignored when writing('\r). The other, more GUI based console windows (IDE's) are obviously implemented differently. So perhaps it could be a feature request to interprete '\r' to erase a previous line in these consoles, so that all consoles share at least a common behaviour to the standard console, only perhaps with additional options.

A solution meanwhile for me as user could be to change the sys.stdout to a new console window, which is questioned before, but answered unsatisfactory... Anybody evolved a bettor opinion to this?

Or use something like the curses module, but than for Windows (is there a 'curses.py' for Windows?), e.g. Console.py.



来源:https://stackoverflow.com/questions/6804521/how-to-remove-lines-from-stdout-in-python-in-scite-idle-eclipse-or-other-edi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!