Sunday 15 April 2012

python - Output to the same line overwriting previous -



python - Output to the same line overwriting previous -

how output same line overwriting previous received timing(countdown) ntp server. shown below after each sec timing receiving in next row.

13:35:01 13:35:00 13:34:59 13:34:58 13:34:57 13:34:56

i want timing should received in same row clearing previous one.

you can use "return"-character \r homecoming origin of line. in python 2.x, you'll have utilize sys.stdout.write , sys.stdout.flush instead of print.

import time, sys while true: sys.stdout.write("\r" + time.ctime()) sys.stdout.flush() time.sleep(1)

in python 3.3, can utilize print function, end , flush parameters:

print(time.ctime(), end="\r", flush=true)

note, however, way can replace lastly line on screen. if want have "live" clock in more complex console-only ui, should check out curses.

import time, curses scr = curses.initscr() scr.addstr(0, 0, "current time:") scr.addstr(2, 0, "hello world!") while true: scr.addstr(0, 20, time.ctime()) scr.refresh() time.sleep(1)

python

No comments:

Post a Comment