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

Eka
  • 14,170
  • 38
  • 128
  • 212
  • You just import it? – Klaus D. Mar 12 '19 at 03:13
  • Don't want to import it. I want it as standard function – Eka Mar 12 '19 at 03:15
  • Also using `os.system` to clear a screen is not cross-platform. See: https://stackoverflow.com/a/23980136/2904896 – metatoaster Mar 12 '19 at 03: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? – Klaus D. Mar 12 '19 at 03:17
  • Also, calling `os.system("echo` will not achieve what you want. Just use the Python built-in print function to achieve what you want on posix: `print(chr(27) + "[2J")` – metatoaster Mar 12 '19 at 03:20
  • @KlausD. I want to use it locally same as this one https://stackoverflow.com/questions/45119983/how-to-define-built-in-function-in-my-python-interpreter – Eka Mar 12 '19 at 03:21
  • @metatoaster Its not about adding empty space but to scroll up and down – Eka Mar 12 '19 at 03:23
  • 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. – metatoaster Mar 12 '19 at 04:05
  • Hey thanks it worked in python3. Its adding empty space above the cursor any way to remove it and add below the cursor. – Eka Mar 12 '19 at 04:13
  • @metatoaster I found the solution thanks to your code `print('\x1b[J\x1b[A\x1b[A\x1b[A'*20, end='')` many thanks :) – Eka Mar 12 '19 at 04:25

0 Answers0