print problem

Peter Pearson ppearson at nowhere.invalid
Wed Jun 18 17:42:42 CEST 2008


On Tue, 17 Jun 2008 04:46:38 -0300, Gabriel Genellina wrote:
> En Tue, 17 Jun 2008 04:10:41 -0300, Rich Healey escribió:
>> Gabriel Genellina wrote:
>>> En Tue, 17 Jun 2008 03:15:11 -0300, pirata <pirata at mars.invalid> escribió:
>>>
>>>> I was trying to print a dot on console every second to indicates
>>>> running process, so I wrote, for example:
>>>>
>>>> for i in xrange(10):
>>>>     print ".",
>>>>     time.sleep(1)
>>>>
>>>> Idealy, a dot will be printed out each second. But there is nothing
>>>> print out until after 10 seconds, all 10 dots come out together.
>>>>
>>>> I've tried lose the comma in the print statement, and it works.
>>>>
>>>> Is that because of the print statement buffer the characters until
>>>> there is a new line character?
[snip]
>> Or just write to sys.stdout without the print wrapper..
>
> I think the output is still buffered, even if you write
> directly to sys.stdout, but I don't have a Linux box to
> test right now.

Here (Python 2.4.3), dots written to sys.stdout are saved up and
appear all at once.

However, you can flush the buffer yourself by invoking sys.stdout.flush()
immediately after either the "print" or the "sys.stdout.write".

-- 
To email me, substitute nowhere->spamcop, invalid->net.



More information about the Python-list mailing list