2

I know you can clear the shell by executing clear using os.system, but this way seems quite messy to me since the commands are logged in the history and are litterally interpreted as commands run as the user to the OS.

I'd like to know if there is a better way to clear the output in a commandline script?

braX
  • 11,506
  • 5
  • 20
  • 33
Joseph
  • 1,003
  • 3
  • 11
  • 25
  • 1
    as an aside ... `clear` is not a cross platform command iirc it will only work in windows (I think `cls` works in linux) – Joran Beasley Dec 21 '15 at 03:28
  • 3
    @JoranBeasley: It's the other way around: `clear`, though not a POSIX utility, is a _utility_ available on most Unix-like platforms; by contrast, `cls` is a _built-in_ command of the Windows command processor, `cmd.exe`. – mklement0 Dec 21 '15 at 03:35
  • 2
    meh I knew it was something like that :P – Joran Beasley Dec 21 '15 at 03:36

3 Answers3

7
print "\033c"

works on my system.

You could also cache the clear-screen escape sequence produced by clear command:

import subprocess
clear_screen_seq = subprocess.check_output('clear')

then

print clear_screen_seq

any time you want to clear the screen.

tput clear command that produces the same sequence is defined in POSIX.

You could use curses, to get the sequence:

import curses
import sys

clear_screen_seq = b''
if sys.stdout.isatty():
    curses.setupterm()
    clear_screen_seq = curses.tigetstr('clear')

The advantage is that you don't need to call curses.initscr() that is required to get a window object which has .erase(), .clear() methods.

To use the same source on both Python 2 and 3, you could use os.write() function:

import os
os.write(sys.stdout.fileno(), clear_screen_seq)

clear command on my system also tries to clear the scrollback buffer using tigetstr("E3").

Here's a complete Python port of the clear.c command:

#!/usr/bin/env python
"""Clear screen in the terminal."""
import curses
import os
import sys

curses.setupterm()
e3 = curses.tigetstr('E3') or b''
clear_screen_seq = curses.tigetstr('clear') or b''
os.write(sys.stdout.fileno(), e3 + clear_screen_seq)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • print "\033c" works... would be nice to know why? – Jacques Jan 27 '21 at 13:16
  • 1
    @Jacques `b’\033c’` is `ESC c` “Reset to Initial State” that is “type Fs” (independent function) ANSI escape sequence recognized by terminals https://en.wikipedia.org/wiki/ANSI_escape_code – jfs Jan 27 '21 at 15:50
  • The specific section of the Wikipedia article referred to by @jfs is "Fs Escape sequences": https://en.wikipedia.org/wiki/ANSI_escape_code#Fs_Escape_sequences – Martijn Dec 28 '21 at 13:23
3

You can use the Python interface to ncurses, specifically window.erase and window.clear.

https://docs.python.org/3.5/library/curses.html

pvg
  • 2,673
  • 4
  • 17
  • 31
1

I use 2 print statements to clear the screen.

Clears the screen:

print(chr(27) + "[2J")

Moves cursor to begining row 1 column 1:

print(chr(27) + "[1;1f")

I like this method because you can move the cursor anywhere you want by [<row>;<col>f

The chr(27) is the escape character and the stuff in quotes tells the terminal what to do.

mklement0
  • 382,024
  • 64
  • 607
  • 775
DustinPianalto
  • 373
  • 1
  • 8
  • Good stuff; for simplification, consider using embedded octal escape sequence `\033` in lieu of synthesizing your string with `chr(27) + ...`; e.g., `print("\033[1;1f")` – mklement0 Dec 21 '15 at 04:24
  • Just curious, why use octal instead of hex? – DustinPianalto Dec 21 '15 at 14:59
  • You can use either; perhaps the octal ones are slightly more readable, given that they're composed of _digits_ only; compare `print('\033c')` to `print('\x1bc')`. – mklement0 Dec 21 '15 at 15:43