1

So obviously you can clear the console with os.system('clear') but this seems like a very bodged solution to me. Is there a more elegant way to clear the console?

I feel that this question is different from Clear terminal in Python because I am not asking simply how to clear the terminal, I am asking which is the most pythonic. Python - Clearing the terminal screen more elegantly is not what I am looking for either, as the marked answer there still does not feel very elegant or pythonic. Using escape characters or calling the command and checking the output seems even more like a bodge than an actual solution.

Zeke Egherman
  • 359
  • 5
  • 17
  • Doing this in a portable way is more difficult than you apparently think. Sorry if none of the (many) existing solutions seem elegant enough... – martineau Feb 25 '19 at 21:25
  • I understand that it's difficult. If it was easy, I wouldn't be asking the question. – Zeke Egherman Feb 25 '19 at 21:33

1 Answers1

0
import curses
stdscr = curses.initscr()
stdscr.erase()

Rather than assuming specific character control codes, the curses package was developed for portable full screen character control in the heyday of many different video terminals, with many different and incompatible control sequences. It also turns the use of such codes from cryptic raw escape sequences to more readily understandable named functions, such as "erase." Look at the curses package documentation to see all of the capabilities. Here, we initialize curses to the (default) full terminal window, then erase the contents of that window.

As for this being Pythonic, it's hard to see something more Pythonic than using a standard Python package, initialize, and one line of code...

mpez0
  • 2,815
  • 17
  • 12