Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a python 3 command to clear the output console?

So I am trying to make a simple Conway's game of life in PyCharm, and I can only get a a bunch outputs and not a video like stream in the output console. Is there a command that will let me clear the output every loop in the program. I have already tried the "sys" commands, and the ANSI escape keys (I hope I spelled that right). Nothing seems to be working! I am using Python 3.

I would like to clear the console on the first print statement in the while loop. If that helps.

import copy
import random
import time
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()
like image 959
Carsten Brooks Avatar asked Oct 16 '25 01:10

Carsten Brooks


2 Answers

From this https://www.jetbrains.com/help/pycharm/interactive-console.html. It basically uses a system based python interpreter, there is no direct way or command to clear Python interpreter console. So you need a system call to clear the Python interpreter console screen. For window system, cls clear the console. For the Linux system, clear command works. It requires the OS library to be imported.

import os clear = lambda: os.system('cls') #on Windows System os.system('clear') #on Linux System clear()

The “lambda” keyword in Python is used to define anonymous functions.

import os is inbuild in python 3

like image 84
HariKishore Avatar answered Oct 17 '25 14:10

HariKishore


For those who are using vs code, then to clear the console output :

import os
    os.system('clear')
like image 41
15_ Dinesh reddy Avatar answered Oct 17 '25 14:10

15_ Dinesh reddy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!