views:

373

answers:

4

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

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.

+3  A: 

Look here

here too for a curses library for python on windows...

Jason Punyon
ANSI doesn't work with Windows XP but \r works as expected. For some reason, it didn't work when I tried five minutes ago. Oh well :)
Aaron Digulla
A: 

My question about a Python Download Progress Indicator might be helpful.

cschol
+1  A: 

yes:

import sys
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()
nosklo
A: 

Simple way if you're just wanting to update the previous line:

import time
for i in range(20):
    print str(i) + '\r',
    time.sleep(1)
Therms