Is there any function or method to clear the running console in python program?

Viewed 44

I am using pyCharm pro on windows.

I tried almost all the methods but no one is working.

I tried these functions too:

def screen_clear():
   # for mac and linux(here, os.name is 'posix')
   if os.name == 'posix':
      _ = os.system('clear')
   else:
      # for windows platfrom
      _ = os.system('cls') 

with this py code

i = 10
while i > 0:
    print("X"*i)
    i -= 1
    sleep(1)
    screen_clear()

and its giving this output: Code Output

1 Answers

If you are using Windows Terminal, this works:

print('\x1bc')

To respond to the comments. I just tested this again with a pristine virtual machine under Windows 10 1903, also using Windows Terminal 1.4 and Python 3.9, and it works as expected.

Related