import sys, os
os.system('cls')
import os
os.system('cls' if os.name == 'nt' else 'clear')
print('33[H33[J', end='')
import os
def clear(): # this function will clear the console
command = 'cls' # cls is for windows
if os.name != 'nt': # if it isnt windows it will use clear
command = 'clear'
os.system(command)
return 0
# example usage:
name = input('whats your name?')
clear()
print('your name is: ' + name)
import os
def clearConsole():
command = 'clear'
if os.name in ('nt', 'dos'): # If Machine is running on Windows, use cls
command = 'cls'
os.system(command)
clearConsole()
import sys
import os
if os.name == 'nt':
os.system('cls')
else:
os.system('clear')
# the reason i used "if os.name == 'nt'" is because the operating system "nt"
# is windows, and windows can only use the command "cls" to clear the
# console, if a linux user is using your program then it'll throw an error
# because only command prompt uses "cls"
import sys
sys.stdout.flush()
import os
os.system('clear')
print("33c", end="")
def clear():
# for windows
if name == 'nt':
_ = system('cls')
# for mac and linux(here, os.name is 'posix')
else:
_ = system('clear')
As you mentioned, you can do a system call:
For Windows
>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()
For Linux the lambda becomes
>>> clear = lambda: os.system('clear')
Ctrl-L Or cls
>>> import os
>>> clear = lambda: os.system('cls')
>>> clear()
print("33[H33[J", end="")
from os import system
from sys import platform
clear_screen = lambda: system("cls" if platform == "win32" else "clear")
clear_screen() # will clear the console
# Cross-platform using appropriate commands for Windows and non-Windows