Search code examples
pythonpython-3.xpycharmos.system

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?


Solution

  • 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.