[Tutor] Printing a progress bar
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sun Jun 13 23:51:27 EDT 2004
On Sun, 13 Jun 2004, Dave S wrote:
> Initualy I tried ...
>
> print 'Progress ',
> ..
> loop
> ...
> print '#',
> ...
>
> But it appears python does not send a line to the console until it can
> send a CR.
Hi Dave,
Right; what's happening is called "buffering". Internally, the system
keeps track of the character to be written out, and saves them in a line
buffer. As soon as we send the system a CR, it'll flush that buffer out
to disk.
(By the way, the same thing happens when we write() to file objects.)
We can manually force a flush by calling the flush() methods on the
standard output file object:
###
def printUnbufferedHash():
print '#',
sys.stdout.flush()
###
> PS can anyone tell me the ASCII code to move the cursor left one
> position so I can write a string of
> ..............................................
> becoming
> #########............................
> as progress is made ?
Ah, you're looking for the backspace character '\b'. No need to remember
the exact ASCII code: use the escape sequence.
But if you're really interested, it's:
###
>>> ord('\b')
8
###
Eight. *grin*
Good luck to you!
More information about the Tutor
mailing list