sys.stdout.write() question

Discussion in 'Python' started by Gian Mario Tagliaretti, Jun 21, 2004.

  1. 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
    Gian Mario Tagliaretti, Jun 21, 2004
    #1
    1. Advertising

  2. Gian Mario Tagliaretti

    Ivan Voras Guest

    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()
    Ivan Voras, Jun 21, 2004
    #2
    1. Advertising

  3. 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
    Reinhold Birkenfeld, Jun 21, 2004
    #3
  4. Gian Mario Tagliaretti

    Peter Hansen Guest

    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
    Peter Hansen, Jun 21, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Roman Neuhauser
    Replies:
    0
    Views:
    643
    Roman Neuhauser
    Apr 4, 2005
  2. Replies:
    2
    Views:
    637
    velle
    Jan 5, 2006
  3. Replies:
    0
    Views:
    446
  4. Mitchell L Model

    sys.stdout vs. sys.stderr

    Mitchell L Model, Jan 11, 2010, in forum: Python
    Replies:
    2
    Views:
    552
    Nobody
    Jan 11, 2010
  5. Michel Albert
    Replies:
    5
    Views:
    1,352
    Daniel Dehennin
    Oct 24, 2012
Loading...

Share This Page