Can't clear terminal with os.system('cls')

C4-unclean.txt (7.2 KB)

Hello there,
I’ve coded a little game of connect 4 just to see if i could do it with my actual knowledge of python (i’m only halfway through the https://www.codecademy.com/learn/learn-python-3 course)

It seems to work okay but i can’t find a way to clear the terminal after each play
i’ve tried the code block :

import os
os.system('cls')

or

import os
os.system('clear')

and noone of those worked.
(I wanted it to clear just before the call of board() in the load_play() function.)

I run it with IDLE 3.7 (x64) if that change something (don’t understand much about ide and terminal currently)

Thanks !

1 Like

This will not work in the IDLE. You can run your script in the system shell and the cls or clear command will work correctly.

Python IDLE is not a normal shell and it does not act like a normal shell :slight_smile: So you are limited to what the IDLE developers have implemented. And from my quick research, it looks like this feature is not available currently, but its implementation was heavily discussed over the past ten years :slight_smile: Here is an issue report -> https://bugs.python.org/issue6143.

You still have some options! You can change the execution environment to PowerShell or CMD. Or you can install the extension for the IDLE that is linked in the issue I mentioned earlier (the file named ClearWindow.py). It was written almost ten years ago so I am not sure if it will work or if that is a good idea, but it is still an option, I think.

8 Likes

Thank you were right !
I’ve updated the code with the clearing blocks and runned by using double click instead of the IDLE and it worked as intended !
Thanks @factoradic !

3 Likes

You’re very welcome :slight_smile:

thanks, this was confusing me too. Do you know if there is a way to keep the system shell open when the code finishes running, because as soon as the code finishes running, the shell closes.

Hi o/

Don’t know if there is a function for that, but I would probably juste use an input() to freeze the script at the end.