How to clear cmd/terminal while running a python script
Question:
I keep seeing ways to clear the shell while running a script, however is there a way to clear the screen while running a script in the CMD?
My current method works like this:
clear.py
import title
def clear():
print('n' * 25)
title.title()
game.py
from engine import clear
clear.clear()
print(Fore.CYAN + Style.BRIGHT + "--------------------------------")
However this method isn’t really reliable as some cmd sizes are different on all computers, nor have I tried it on OSx.
Answers:
Your best bet is probably to use the colorama module to enable ANSI escape sequences in the Windows terminal, and then use the ANSI sequence to clear the screen:
import colorama
colorama.init()
print(" 33[2J 33[1;1f")
This should work on all common platforms.
import os
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)
I keep seeing ways to clear the shell while running a script, however is there a way to clear the screen while running a script in the CMD?
My current method works like this:
clear.py
import title
def clear():
print('n' * 25)
title.title()
game.py
from engine import clear
clear.clear()
print(Fore.CYAN + Style.BRIGHT + "--------------------------------")
However this method isn’t really reliable as some cmd sizes are different on all computers, nor have I tried it on OSx.
Your best bet is probably to use the colorama module to enable ANSI escape sequences in the Windows terminal, and then use the ANSI sequence to clear the screen:
import colorama
colorama.init()
print(" 33[2J 33[1;1f")
This should work on all common platforms.
import os
os.system(‘cls’ if os.name == ‘nt’ else ‘clear’)