0

I want to add a clear screen function to the standard function list (without importing it) such as print on python. When I invoke clear in python it should clear the screen. How can I do it?

import os
def clear():
    os.system("clear")
    #os.system('echo -ne "\eD\eD\eD\e[A\e[A\e[A"')

Secondly any idea why this is not working os.system('echo -ne "\eD\eD\eD\e[A\e[A\e[A"')

Edit:

Solution for os.system('echo -ne "\eD\eD\eD\e[A\e[A\e[A"') is print('\x1b[J\x1b[A\x1b[A\x1b[A'*20, end='')(py3) whole credit to this code goes to @metatoaster

10
  • You just import it? Commented Mar 12, 2019 at 3:13
  • Don't want to import it. I want it as standard function Commented Mar 12, 2019 at 3:15
  • Also using os.system to clear a screen is not cross-platform. See: stackoverflow.com/a/23980136/2904896 Commented Mar 12, 2019 at 3:17
  • That's not how it should be done. How would an other developer looking at your code know where clear comes from and where it is documented? Commented Mar 12, 2019 at 3:17
  • 1
    @Eka you can't achieve that using echo from within Python, and use print and replace all \e with chr(27) much like how I indicated using the example. Alternatively use \x1b instead, so you would replace that whole "not working" statement with print('\x1b[2J\x1bD\x1bD\x1bD\x1b[A\x1b[A\x1b[A', end='') to both clear screen and shift the lines as you indicated. Commented Mar 12, 2019 at 4:05

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.