Python: display refreshing status (like top)
In scripts I often want to display some status information (e.g. progress), this can be achieved with e.g.:
PYTHON:
-
print "progress: %i %%\r" % i,
-
sys.stdout.flush()
but this just works for one-liners. I wanted to have what top or less do: open a new "window" and being able to write everywhere in the window, not just on the last line.
Found out that curses does exactly that - but I didn't find a stripped-to-the-bare-necessities-example, so here you are:
PYTHON:
-
import curses, time
-
from datetime import datetime
-
w = curses.initscr()
-
try:
-
while True:
-
w.erase()
-
w.addstr("some status..\ncurrent time\n%s" % datetime.now())
-
w.refresh()
-
time.sleep(1)
-
finally:
-
curses.endwin()