Printing repeatedly on same line

Mike Meyer mwm at
Sat Jan 25 22:10:48 CET 2003

Jason Morgan <jwm-python at> writes:

> How do I print (stdout) to the same line repeatedly? I want to offer a
> status bar for a script. I have tried the following:
> def statprint(s):  # s is a string
> 	import sys
> 	sys.stdout.write(s+" "*(78-len(s))+"\r")
> I got this from this ASPN page:
> which seemed to be refering to Windows (I'm writing for Linux/*BSD). I
> have tried the above function, but it prints in the same manner as
> 'print' does.

I know you have better answers, but thought I'd give you an
explanation for why this is not working.

Unix terminal output drivers can be set to map carriage return to
newline (the ocrnl flag on FreeBSD). You probably have that set. I
don't, and your example worked just fine for me. You need to change
that setting in your program to make it work properly.

Mike Meyer <mwm at>
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.

More information about the Python-list mailing list