1

I know there's tons of libraries out there that does this for you, but I want to know if there's any simple way that's built in.

So far I'm just doing os.system('clear'). This works, but the 'clear' command only inserts a bunch of newlines instead of actually clearing the whole screen.

This is fine for animations that aren't very big, but for animations that cover up more than 1 screen worth of space, it will start scrolling up and down.

Razor Storm
  • 12,167
  • 20
  • 88
  • 148
  • 2
    some Unix/Linux terminals can use special codes to change color, move cursor and also clear terminal. And Python module like `curses`, `pycurses`, use them. But not all terminals may have to respecpect these codes. Wikipedia [ANSI escape code](https://en.wikipedia.org/wiki/ANSI_escape_code) – furas May 03 '20 at 20:48
  • 2
    Duplicate of [How to clear the interpreter console?](https://stackoverflow.com/questions/517970/how-to-clear-the-interpreter-console) – Jongware May 03 '20 at 21:08
  • The answers in that question are explicitly what I stated in my question: using the os.system('clear') which I mentioned doesn't work for my usecase. So maybe my question doesn't have an answer, but it's def not the same question as what those other people were answering – Razor Storm May 08 '20 at 18:47
  • After doing some digging and *actually reading* the short 4 sentence question I wrote, I found that this is the actual duplicate question: https://stackoverflow.com/questions/5367068/clear-a-terminal-screen-for-real – Razor Storm May 08 '20 at 19:08

1 Answers1

0

First of all, your os.system('clear') can be made cross-platform by doing

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

Nevertheless, you may want to try an ANSI escape sequence:

print("\033c", end="\033[A")

It is worth mentioning that to make this approach cross-platform, the terminal must accept ANSI code. A typical windows terminal does not. So, you can use colorama as so:

if os.name == "nt":  # Detect if Windows
    import colorama
    colorama.init()  # Activate ANSI escape sequences

There is also:

printf '\33c\e[3J'
YulkyTulky
  • 886
  • 1
  • 6
  • 20
  • The point isn't cross platform. The point is 'clear' doesn't clear the terminal at all. It just adds a bunch of new lines. – Razor Storm May 04 '20 at 21:47
  • @RazorStorm I gave multiple solutions, one happening to have cross-platform compatibility. This is an anti-problem – YulkyTulky May 04 '20 at 22:52
  • I tried the others just now, neither `print("\033c", end="\033[A")` nor `printf '\33c\e[3J'` worked. I tried print(f'\33c\e[3J') or print('\33c\e[3J') or print(r'\33c\e[3J') as a raw string. None of these worked. It didn't clear the page at all. os.system('clear') simply adds new lines. The last option is on windows, and this is on OSX – Razor Storm May 08 '20 at 18:46
  • interestingly enough, when I run `print('\33c\e[3J')` in python interpreter in terminal it works. however when I run it as part of my script it doesn't. looking into that now – Razor Storm May 08 '20 at 18:49
  • Ah, so when the script outputs anything that takes up more than one screen's worth of rows, printf '\33c' will only clear the content on the current page. All previous page contents are kept. Looking up a way to extend this – Razor Storm May 08 '20 at 18:59