command line reports

Bengt Richter bokr at
Thu Aug 11 16:42:57 EDT 2005

On Thu, 11 Aug 2005 15:43:23 -0400, Darren Dale <dd55 at> wrote:

>Peter Hansen wrote:
>> Darren Dale wrote:
>>> Is there a module somewhere that intelligently deals with reports to the
>>> command line? I would like to report the progress of some pretty lengthy
>>> simulations, and currently I have the new reports written on a new line
>>> rather rather than overwriting the previous report.
>> You mean you want sys.stdout.write(report + '\r') instead of "print
>> report" ?
>> It's not really clear what you want.  What's a "report" to you?
>> -Peter
>I am printing something like 
>trial 1 of 100
>trial 2 of 100
Peter's suggestion will work, but it's easy to get something like

 >>> import sys, time
 >>> def test():
 ...     for i in xrange(5):
 ...         sys.stdout.write(('trial %s of 5'%(i+1)) + '\r')
 ...         time.sleep(.25)
 ...     print "We're done!"
 >>> test()
 We're done!5
            ^--(note leftover from last "report: line)
By the same token (more or less ;-) if your "report" line shortens
during progress (e.g., changing from

    'long_name_test trial 100 of 100'

    'short_name trial 1 of 10'

you will likely see

    'short_name trial n of 10 of 100'

for a while, unless you pad the report line with blanks to a constant length.

Also, I guess some platforms could require flushing stdout to be sure of
seeing the latest line as you go.

>so I get 100 lines by the time the code is finished. I would like to replace
>the previous report with the current one, so I only use one line by the
>time the code is finished.
>I was also hoping that there was a set of tools out there for spinners,
>meters, etc...
The question with python for this kind of thing is usually, "Which will be
faster, googling for it, or writing it?" ;-)

Bengt Richter

More information about the Python-list mailing list