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 works on all platforms and it does work in both Python 2 and 3.
Then to clear just type
clear(numberhere)
.You could try to rely on clear but it might not be available on all Linux distributions. On windows use cls as you mentionned.
Note: It could be considered bad form to take control of the terminal screen. Are you considering using an option? It would probably be better to let the user decide if he want to clear the screen.
This function works in gnome-terminal because, by default, it recognizes ANSI escape sequences. It gives you a CLEAN PROMPT
rows_max
distance from the bottom of the terminal, but also precisely from where it was called. Gives you complete control over how much to clear.Implementation:
Parameters:
rows
is the number of clear text rows to add between prompt and bottom of terminal, pushing everything up.rows_max
is the height of the terminal (or max clearing height) in text rows, and only needs to be set once, but can be reset at any time.*,
in the third parameter position means all following parameters are keyword only (e.g., clear(absolute=5)).calling_line=True
(default) works better in Interactive mode.calling_line=False
works better for text-based, terminal applications.absolute
was added to try to fix glitchy gap problems in Interactive mode after reducing size of terminal, but can also be used for terminal applications.store_max
is just for secret, "persistent" storage ofrows_max
value; don't explicitly use this parameter. (When an argument is not passed forstore_max
, changing the list contents ofstore_max
changes this parameter's default value. Hence, persistent storage.)Portability: Sorry, this doesn't work in IDLE, but it works >> VERY COOL << in Interactive mode in a terminal (console) that recognizes ANSI escape sequences. I only tested this in Ubuntu 13.10 using Python 3.3 in gnome-terminal. So I can only assume portability is dependant upon Python 3.3 (for the
shutil.get_terminal_size()
function for BEST results) and ANSI recognition. Theprint(...)
function is Python 3. I also tested this with a simple, text-based, terminal Tic Tac Toe game (application).For use in Interactive mode: First copy and paste the
copy(...)
function in Interactive mode and see if it works for you. If so, then put the above function into a file named clear.py . In the terminal start python, with 'python3'. Enter:Now drop the clear.py file into one of the
path
directories listed so that Python can find it (don't overwrite any existing files). To easily use from now on:For use in a terminal application: Put the
copy(...)
function into a file named clear.py in the same folder with your main.py file. Here is a working abstract (skeleton) example from a Tic Tac Toe game application (run from terminal prompt: python3 tictactoe.py):Explanation:
do_print(...)
on line 19 is a version ofprint(...)
needed to keep track of how many new lines have been printed (self.rows
). Otherwise, you would have toself.rows += 1
all over the place whereprint(...)
is called throughout the entire program. So each time the board is redrawn by callingshow_board()
the previous board is cleared out and the new board is printed exactly where it should be. Noticeself.clear(calling_line=False)
on line 9 basically pushes everything up RELATIVE to the bottom of the terminal, but does not clear the original calling line. In contrast,self.clear(absolute=self.rows)
on line 29 absolutely clears out everythingself.rows
distance upward, rather than just pushing everything upward relative to the bottom of the terminal.Ubuntu users with Python 3.3: Put
#!/usr/bin/env python3
on the very first line of the tictactoe.py file. Right click on the tictactoe.py file => Properties => Permissions tab => Check Execute: Allow executing file as program. Double click on the file => Click Run in Terminal button. If an open terminal's current directory is that of the tictactoe.py file, you can also start the file with./tictactoe.py
.A perhaps cheesy way to clear the screen, but one that will work on any platform I know of, is as follows:
Just use:
This will clear the terminal window.
As a wise person once posted, for at least Linux, you can do the Python equivalent of this, which you can use on the command-line. It will clear the screen more than the Linux
clear
command (such that you can't scroll up to see your text):For Windows you can just use
cls
.Here's a Python way to do it:
Here's another Python way to do it (that works on at least my computer):
I have no idea if that works in Windows or on Mac, iOS, Android, etc., though.
You can use the other people's answers to figure out a more cross-platform way to implement this.
As rolika said in the comments, you can do this to prevent a new line from being printed: