Does any standard "comes with batteries" method exist to clear the terminal screen from a Python script, or do I have to go curses (the libraries, not the words)?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
This will clear 25 new lines:
I use eclipse with pydev. I like the newline solution better than the for num in range . The for loop throws warnings, while the print newline doesn't. If you want to specify the number of newlines in the clear statement try this variation.
If you are on a Linux/UNIX system then printing the ANSI escape sequence to clear the screen should do the job. You will also want to move cursor to the top of the screen. This will work on any terminal that supports ANSI.
This will not work on Windows unless ANSI support has been enabled. There may be an equivalent control sequence for Windows, but I do not know.
You can use
call()
function to execute terminal's commands :you can make your own. this will not be dependent on your terminal, or OS type.
I would do it in this way to make it look more like bash:
Just create a file named .pythonstartup at Home directory and use poke's answer in a function
On Linux:
You can add
export PYTHONSTARTUP=$HOME/.pythonstartup
to your./bashrc
fileSince what I care about is space; a call to the function will not display the python interpreter description at startup, but you can remove
clear()
to retain it.Using it like a normal function should do the trick without printing the exit status:
If you pass the argument 0 to the function it will clear the screen and exit successfully so you can continue using the shell in a clean screen
So just thought I would throw my two cents in here...
No one has provided a true answer to OP question it seems, everyone either responds with 'NO DONT USE os.system() it's evil!!!' without explanation or provides a solution that relies on printing new lines.
For those that need to clear the terminal screen and scroll back, for whatever reason, you can use the following code:
This has the OP's desired effect. It does use the os.system() command so if that's evil and someone knows a way of implementing this using subprocess.call() please comment as I would also prefer to use subprocess but am not familiar with it at all.