sleep() function, perhaps.

Isaac To kkto at csis.hku.hk
Tue Nov 25 03:30:14 EST 2003


>>>>> "Ryan" == Ryan Spencer <jeder at earthlink.net> writes:

    Ryan> On Tue, 25 Nov 2003 06:14:20 +0000, Christopher Koppler wrote:
    >> On Tue, 25 Nov 2003 05:26:25 GMT, Ryan Spencer <jeder at earthlink.net>
    >> wrote:
    >> 
    >>>  Hello Everyone,
    >>> 
    >>> I want to have a row of periods, separated by small, say, .5 second
    >>> intervals between each other. Thus, for example, making it have the
    >>> appearance of a progress "bar".
    >>> 
    >>> [code] import time
    >>> 
    >>> sleep(.5) print "."  sleep(.5) print "."  [end code]
    >>> 
    >>> But, it would (with those .5 second intervals) print out much like
    >>> the following.
    >>> 
    >>> .  (pause) .  (pause)
    >>> 
    >>> I would rather those periods be on a single line, not printing on a
    >>> new line each time.
    >>> 
    >>> Any suggestions?
    >>  Try print with added comma or sys.stdout.write, like so:
    >> 
    >>>>> import time for i in range(10):
    >> ...  print '\b.', ...  time.sleep(1.5) ...  ..........
    >>>>> import sys for i in range(10):
    >> ...  sys.stdout.write('.')  ...  time.sleep(0.5) ...  ..........


    Ryan> Heya', Thanks,

    Ryan> Actually though, None of those suggestions give me the desired
    Ryan> result I was looking for. I used both with the for loops, even the
    Ryan> one with the while loop, and for the first suggested it prints all
    Ryan> of them out on new lines (as opposed to all on the same line as
    Ryan> I'd been hoping for) and the second posts on one full line, yet,
    Ryan> the periods still don't have pauses between themselves. Perhaps
    Ryan> something else is amiss?

    Ryan> As well, the trailing commas gives the exact same result as doing
    Ryan> the sys.stdout.write function.

    Ryan> Is the code that you suggested giving you a result such as...

    Ryan> .(pause).(pause).(pause).

    Ryan> I raised everything up to a 1.5 second interval to exaggerate the
    Ryan> results, and I'm afraid I still don't notice the pauses.

    Ryan> Perchance I simply need to remove whatever is terminating the
    Ryan> line?  Does the time.sleep() function itself terminate a line? It
    Ryan> would seem if I could bypass that, it would allow the pauses and
    Ryan> keep the periods on one line.

    Ryan> Thank you for your advice though, It's highly appreciated.

Without a newline character, the normal sys.stdout writes to a buffer and
won't try to flush it out.  Try this:

>>> for i in range(10):
...   sys.stdout.write('.')
...   sys.stdout.flush()
...   time.sleep(.5)
... 
..........>>> 

Regards,
Isaac.




More information about the Python-list mailing list