Bryan
Bryan

Reputation: 87

Why doesn't os.system('cls') clear the recent output?

I am always using system('cls') in C language before using Dev-C++. Now I am studying Python and using Pycharm 2020.2.3.

I tried to use os.system('cls'). Here is my program:

import os

print("clear screen")
n = int(input(""))
if n == 1:
    os.system('cls')

There is no error in my program but it is not clearing the recent output.

This is the output of my program:

screenshot of my output

What seems to be the problem why it is not clearing the recent output?

Upvotes: 8

Views: 15472

Answers (3)

AndrewTheSkids Alt
AndrewTheSkids Alt

Reputation: 1

here is an example:

import time
import os

while True:
    os.system('cls')
    age = int(input('how old are you?'))
    if age >= 18:
        print('you are at the legal age in America to vote!')
    else:
        print('you cannot vote!')
    time.sleep(3)

Upvotes: -1

Paul Randleman
Paul Randleman

Reputation: 114

PhCharm displays the output of your running module using the output console. In order for your terminal commands under os.system() to work, you need to emulate your terminal inside the output console.

  1. Select 'Edit Configurations' from the 'Run' menu.
  2. Under the 'Execution' section, select 'Emulate terminal in output console'

JetBrains Sergey Karpov Adds:

Our Run window doesn't support some of the things that one can do in the terminal. One of them is clearing the output.

When trying to clear the output in a 'non-terminal' output window, PyCharm even shows that the TERM environment variable is not set.

Setting that variable manually may help in some cases, but other terminal-specific things are still missing, which is why we have an option to emulate the terminal in the Run window.

Upvotes: 8

satvik
satvik

Reputation: 103

This code works perfectly well. I think there is a problem how you are running it. you should run it with commmand prompt.

if still not working.

os.system('cls')  # on windows
os.system('clear')  # on linux / Mac

You can test your code here https://www.python.org/shell/

Upvotes: 3

Related Questions