1

What do I need to do if I want to clear the console without OS-Limitations? I know that on Linux and Mac the command "clear" exists, whereas Windows has "cls". I want to clear the console every now and then on the three major systems without personally choosing "clear" or "cls".

My idea so far was

import platform
os = platform.system()
if os == "Windows":
    clear = 'cls'
else:
    clear = 'clear'

and then just use clear as variable for both, depending on the OS, but it doesn't work. Is something like this actually possible?

Kasai kemono
  • 134
  • 9
  • Seems there is no better way, just different syntactical variations of what you've proposed - see the duplicate and other questions, which should be easy to find. – BartoszKP Nov 19 '18 at 10:53

1 Answers1

0

For accuracy use sys and as a good practice use subprocess

#usr/bin/env python

from sys import platform
from subprocess import run

command = {'win32': 'cls', 'linux': 'clear'}

if __name__ == '__main__':
     run(command[platform], shell=True)

As BartoszKP said there are many ways of doing this, I find this a very clean way of doing it.

Surister
  • 132
  • 3
  • 6