Print always puts newline (or adds a space)

Erik Max Francis max at alcyone.com
Tue Sep 16 15:43:46 EDT 2003


Tobias Pfeiffer wrote:

> Is it then also possible to "jump back" a few chars to, e.g., have a
> progress... (damn, I forgot that word) however, that shows the
> percentage
> of progress? Like print "25%" and then jump back three chars and write
> "26%"?

That's typically done with printing BS characters to back up one space
('\b') or CR characters to return the carriage to the beginning of the
line ('\r').  Note that strictly speaking these may not have the desired
effect, although in most environments they will.

> And then, on my Linux machine, such things just don't happen at all.
> E.g.
> in a loop like...
> 
> for i in range(1000):
>         j = pow(2,i)
>         if j%100: print "#",
> 
> ...I will get nothing for a long time and then ten "#" chars at one
> time.
> Is there a solution for that?

You're encounting buffering; use sys.stdout.write directly and call
sys.stdout.flush() after you've printed some partial output.

-- 
   Erik Max Francis && max at alcyone.com && http://www.alcyone.com/max/
 __ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/  \ Custom reconciles us to everything.
\__/  Edmund Burke




More information about the Python-list mailing list