Alex Rivera | Logout

How can I overwrite/print over the current line in Windows command line?

Asked 2009-01-21T13:43:17.600
31

On Unix, I can either use \r (carriage return) or \b (backspace) to overwrite the current line (print over text already visible) in the shell.

Can I achieve the same effect in a Windows command line from a Python script?

I tried the curses module but it doesn't seem to be available on Windows.

Edit
Report

1 Answer

36

yes:

import sys
import time

def restart_line():
    sys.stdout.write('\r')
    sys.stdout.flush()

sys.stdout.write('some data')
sys.stdout.flush()
time.sleep(2) # wait 2 seconds...
restart_line()
sys.stdout.write('other different data')
sys.stdout.flush()
answered 2009-01-21T14:15:10.657

Your Answer