2

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.

dawsondiaz
  • 674
  • 1
  • 7
  • 13
  • related: [Python - Clearing the terminal screen more elegantly](https://stackoverflow.com/q/34388390/4279) – jfs Sep 07 '17 at 07:36

2 Answers2

3

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("\033[2J\033[1;1f")

This should work on all common platforms.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
  • 1
    In trying to do this, I get the error: `AttributeError: 'module' object has no attribute 'COORD'` – dawsondiaz Mar 01 '14 at 01:24
  • sgarza62's solution has a different effect from your when done in my interpreter on Ubuntu. Yours leaves a 0 at top left and then my prompt on the next line, his completely clears, but progressively moves my prompt down about two lines, thoughts? – Russia Must Remove Putin Mar 01 '14 at 01:27
  • @dawsondiaz: Then your module installation is somehow broken. I can't really help you with that. Try using `pip` to install modules. – Sven Marnach Mar 01 '14 at 01:27
  • @AaronHall: Your probably got those mixed up. I suspect you are trying this in the interactive interpreter, and the zero you are seeing for sgarza62's solution is the exit code of the command, which is only printed because the interactive pronmpt implicitly prints the results of all expression statements. My code only contained the escape sequence for clearing the screen. I added the code for moving the cursor to the top left corner. – Sven Marnach Mar 01 '14 at 01:34
  • @dawsondiaz It appears what you were seeing is a known bug as per their Google Code bug tracker: [Issue 46](https://code.google.com/p/colorama/issues/detail?id=46) – Neil Dec 03 '14 at 00:05
3

Here's another way, that handles Windows cases as well as Unix-like systems (Linux, OSX, etc.):

import os
os.system('cls' if os.name == 'nt' else 'clear')

The clear command works in all Unix-like systems (ie, OSX, Linux, etc.). The Windows equivalent is cls.

sgarza62
  • 5,998
  • 8
  • 49
  • 69
  • 1
    The OP asked specifically for Windows. I gues `call(["cls"])` wouldn't work since `cls` is an internal command of `cmd.exe`. `call("cls", shell=True)` would probably work, but what do I know about Windows. – Sven Marnach Mar 01 '14 at 01:26
  • @SvenMarnach I don't see where he asked for Windows. But yes, you'd still call `subprocess.call()`. – sgarza62 Mar 01 '14 at 01:29
  • "...while running a script in the CMD". I guess that means Windows. – Sven Marnach Mar 01 '14 at 01:34
  • @SvenMarnach True. I've edited my answer. Works on Linux, Mac, and Windows. – sgarza62 Mar 01 '14 at 01:36