Print always puts newline (or adds a space)

Duncan Booth duncan at NOSPAMrcp.co.uk
Tue Sep 16 10:51:27 EDT 2003


Tobias Pfeiffer <BoteDesSchattens at web.de> wrote in
news:bk77de$pt7i4$1 at ID-162581.news.uni-berlin.de: 

>> Yep, call sys.stdout.write directly.
> 
> 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%"?

Back three characters: "\b\b\b"
Back to start of line: "\r"

e.g.
>>> for i in range(10000):
...             j = pow(2,i)
...             if j%100:
...                 sys.stdout.write("\b"+"/-\\|"[i%4])
...

> 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?

Maybe sys.stdout.flush()?


-- 
Duncan Booth                                             duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?




More information about the Python-list mailing list