How to clear the interpreter console?
As you mentioned, you can do a system call:
For Windows:
>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()
For Linux it would be:
>>> import os
>>> clear = lambda: os.system('clear')
>>> clear()
Is there a python 3 command to clear the output console?
On the command prompt (not PyCharm console), try the colorama
library to move the cursor back up and print the next iteration over the current iteration (colorama
makes ANSI control codes compatible with Windows):
(colorama
can be installed via pip install colorama
)
import copy
import random
import time
import colorama
colorama.init()
WIDTH = 60
HEIGHT = 10
nextCells = []
for x in range(WIDTH):
column = []
for y in range(HEIGHT):
if random.randint(0, 1) == 0:
column.append('#')
else:
column.append(' ')
nextCells.append(column)
while True:
#print('\n\n\n\n')
currentCells = copy.deepcopy(nextCells)
for y in range(HEIGHT):
for x in range(WIDTH):
print(currentCells[x][y], end='')
print()
for x in range(WIDTH):
for y in range(HEIGHT):
leftCoord = (x - 1) % WIDTH
rightCoord = (x + 1) % WIDTH
aboveCoord = (y - 1) % HEIGHT
belowCoord = (y + 1) % HEIGHT
numNeighbors = 0
if currentCells[leftCoord][aboveCoord] == '#':
numNeighbors += 1
if currentCells[x][aboveCoord] == '#':
numNeighbors += 1
if currentCells[rightCoord][aboveCoord] == '#':
numNeighbors += 1
if currentCells[leftCoord][y] == '#':
numNeighbors += 1
if currentCells[rightCoord][y] == '#':
numNeighbors += 1
if currentCells[leftCoord][belowCoord] == '#':
numNeighbors += 1
if currentCells[x][belowCoord] == '#':
numNeighbors += 1
if currentCells[rightCoord][belowCoord] == '#':
numNeighbors += 1
if currentCells[x][y] == '#' and (numNeighbors == 2 or numNeighbors == 3):
nextCells[x][y] = '#'
elif currentCells[x][y] == ' ' and numNeighbors == 3:
nextCells[x][y] = '#'
else:
nextCells[x][y] = ' '
# Here we move the cursor back up:
print(f'\033[{HEIGHT+1}A')
time.sleep(1)
How to clear python interpreter screen
>>> import os
>>> os.system("cls")
Should work on windows.
If you are using Linux, you might use clear
instead of cls
.
Clear terminal in Python
What about escape sequences?
print(chr(27) + "[2J")
Clear screen in shell
For macOS/OS X, you can use the subprocess module and call 'cls' from the shell:
import subprocess as sp
sp.call('cls', shell=True)
To prevent '0' from showing on top of the window, replace the 2nd line with:
tmp = sp.call('cls', shell=True)
For Linux, you must replace cls
command with clear
tmp = sp.call('clear', shell=True)
Clear python console command history
Assuming you want to clear the command history Python goes through when you hit the up and down arrow keys, that history is managed by either the GNU readline library or libedit, depending on your system. The Python readline
module is the Python-level interface to the underlying library (even if that library is libedit), and on systems where the underlying library supports it, you can clear the history with readline.clear_history
:
>>> import readline
>>> readline.clear_history()
I do not know if the library on your Mac supports it.
How to clear the interpreter console?
As you mentioned, you can do a system call:
For Windows:
>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()
For Linux it would be:
>>> import os
>>> clear = lambda: os.system('clear')
>>> clear()
How to clear the screen of Interactive IPython console from Python code
(Spyder maintainer here) There are two ways to get what you want:
- You can go to the menu
Run > Configuration per file
and select the option calledExecute in a dedicated console
. That option will clean your console after every execution. Add the following code to your file:
from IPython import get_ipython
get_ipython.run_line_magic('clear', '')
Related Topics
How to Execute a String Containing Python Code in Python
Remove All the Elements That Occur in One List from Another
How to Print a Single Backslash
How to Select a Variable by (String) Name
Pip Install from Git Repo Branch
Choosing the Correct Upper and Lower Hsv Boundaries For Color Detection With'Cv::Inrange' (Opencv)
How to Get All Subsets of a Set - Powerset
How to Get the Last Element of a List
What Does It Mean If a Python Object Is "Subscriptable" or Not
String Count With Overlapping Occurrences
How to Wait Some Time in Pygame
What Is the Python Equivalent of Static Variables Inside a Function
Multiple Aggregations of the Same Column Using Pandas Groupby.Agg()