Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > sys.stdout.write() question

Reply
Thread Tools

sys.stdout.write() question

 
 
Gian Mario Tagliaretti
Guest
Posts: n/a
 
      06-21-2004
Hi all,

It is (I hope) a simple question,
I cannot figure out why sys.stdout.write() doesn't print immediatly the first
text in the small example below, but before it process the code in between
and then print both lines in one time.

#!/usr/bin/env python
import sys, time
sys.stdout.write('write ')
time.sleep(3)
sys.stdout.write('this\n')

if you try to run this, before it will wait 3 seconds and then print "write
this" in one time.

If I put \n here :

sys.stdout.write('write \n')

it work properly but I would like to print the text in one row.

thanks to all
Mario

 
Reply With Quote
 
 
 
 
Ivan Voras
Guest
Posts: n/a
 
      06-21-2004
Gian Mario Tagliaretti wrote:

> #!/usr/bin/env python
> import sys, time
> sys.stdout.write('write ')
> time.sleep(3)
> sys.stdout.write('this\n')
>
> if you try to run this, before it will wait 3 seconds and then print "write
> this" in one time.
>
> If I put \n here :
>
> sys.stdout.write('write \n')
>
> it work properly but I would like to print the text in one row.


It line-buffers the output. If you need to print something that is not 'a
whole line', use sys.stdout.flush() after the ...write()

 
Reply With Quote
 
 
 
 
Reinhold Birkenfeld
Guest
Posts: n/a
 
      06-21-2004
Gian Mario Tagliaretti wrote:
> Hi all,
>
> It is (I hope) a simple question,
> I cannot figure out why sys.stdout.write() doesn't print immediatly the first
> text in the small example below, but before it process the code in between
> and then print both lines in one time.
>
> #!/usr/bin/env python
> import sys, time
> sys.stdout.write('write ')
> time.sleep(3)
> sys.stdout.write('this\n')
>
> if you try to run this, before it will wait 3 seconds and then print "write
> this" in one time.


Apparently the stdout "file" is buffered on your system. Have a look at
file.flush() which could help you out.

Reinhold

--
Wenn eine Linuxdistribution so wenig brauchbare Software wie Windows
mitbrächte, wäre das bedauerlich. Was bei Windows der Umfang eines
"kompletten Betriebssystems" ist, nennt man bei Linux eine Rescuedisk.
-- David Kastrup in de.comp.os.unix.linux.misc
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      06-21-2004
Gian Mario Tagliaretti wrote:

> It is (I hope) a simple question,
> I cannot figure out why sys.stdout.write() doesn't print immediatly the first
> text in the small example below, but before it process the code in between
> and then print both lines in one time.


sys.stdout.flush(). Flush on a file object pushes out all the data
that has been buffered to that point.

-Peter
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments