print, sys.stdout & % complete HELP

greg Landrum greglandrum at earthlink.net
Wed May 24 16:01:42 CEST 2000


bragib at my-deja.com wrote:
> 
> Hi:
> 
> I am trying to print a status message to a terminal window which tells
> the user percent complete.
> 
> This is the way I went about it in pseudo terms
> 
> def percentComplete(lineNo, lines, fileName):
>     msg = 'Scanning file %s, %i%s complete.'
>     percentComplete = int(round(100*lineNo/lines))
>     if divmod(percentComplete, 5)[1]:  # every 5%
>         backspace = '\b'*len(oldStatus)
>         msg = backspace + msg %(fileName, percentComplete, '%')
>         print msg,
>         oldStatus = msg
> 
> Now I have the problem as follows:
> prompt>Scanning file longLongLongName.fil, 100% complete.
> 
> Then when I start scanning a file witha shorter name I
> get
> prompt>Scanning file short.fil, 20% complete. ete.  <--- Oops something
> did not get erased.

I could not get things to work properly with print (I assume this is due
to buffering problems) under kdeterm, but the following works just fine
for me:

for i in xrange(100):
    sys.stdout.write('\rDoing %d'%i)
    sys.stdout.flush()
    time.sleep(.1)

-greg

-- 

greg Landrum (greglandrum at earthlink.net)
Software Carpenter/Computational Chemist



More information about the Python-list mailing list