sys.stdout is not flushed

Discussion in 'Python' started by Jankins, Nov 23, 2009.

  1. Jankins

    Jankins Guest

    I am trying to use sys.stdout to print out "process-bar" like:
    -->1%

    Here is my program ‘test.py’:

    from sys import stdout
    for v in range(10):
    stdout.write('-->%d' % v)
    stdout.flush()
    else:
    stdout.write('done!')
    #end for

    Then, I use 'python -u test.py' to run this script. But what I get
    is :
    -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!

    I am suppose to get 'done!'.

    Can anybody help me about this?

    Thanks.

    Jankins
    Jankins, Nov 23, 2009
    #1
    1. Advertising

  2. Jankins schrieb:
    > I am trying to use sys.stdout to print out "process-bar" like:
    > -->1%
    >
    > Here is my program ‘test.py’:
    >
    > from sys import stdout
    > for v in range(10):
    > stdout.write('-->%d' % v)
    > stdout.flush()
    > else:
    > stdout.write('done!')
    > #end for
    >
    > Then, I use 'python -u test.py' to run this script. But what I get
    > is :
    > -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!
    >
    > I am suppose to get 'done!'.
    >
    > Can anybody help me about this?


    You misunderstand what "flush" means. It is not about clearing the
    screen, or the line.

    Try printing

    stdout.write('\r-->%d')

    Diez
    Diez B. Roggisch, Nov 23, 2009
    #2
    1. Advertising

  3. Jankins

    Jankins Guest

    On Nov 23, 4:08 pm, "Diez B. Roggisch" <> wrote:
    > Jankins schrieb:
    >
    >
    >
    >
    >
    > > I am trying to use sys.stdout to print out "process-bar" like:
    > > -->1%

    >
    > > Here is my program ‘test.py’:

    >
    > > from sys import stdout
    > > for v in range(10):
    > >     stdout.write('-->%d' % v)
    > >     stdout.flush()
    > > else:
    > >     stdout.write('done!')
    > > #end for

    >
    > > Then, I use 'python -u test.py' to run this script. But what I get
    > > is :
    > > -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!

    >
    > > I am suppose to get 'done!'.

    >
    > > Can anybody help me about this?

    >
    > You misunderstand what "flush" means. It is not about clearing the
    > screen, or the line.
    >
    > Try printing
    >
    >    stdout.write('\r-->%d')
    >
    > Diez


    It works. I did misunderstood the meaning the 'flush'.
    Thanks so much.
    Jankins, Nov 23, 2009
    #3
  4. Jankins

    Jankins Guest

    On Nov 23, 4:08 pm, "Diez B. Roggisch" <> wrote:
    > Jankins schrieb:
    >
    >
    >
    >
    >
    > > I am trying to use sys.stdout to print out "process-bar" like:
    > > -->1%

    >
    > > Here is my program ‘test.py’:

    >
    > > from sys import stdout
    > > for v in range(10):
    > >     stdout.write('-->%d' % v)
    > >     stdout.flush()
    > > else:
    > >     stdout.write('done!')
    > > #end for

    >
    > > Then, I use 'python -u test.py' to run this script. But what I get
    > > is :
    > > -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!

    >
    > > I am suppose to get 'done!'.

    >
    > > Can anybody help me about this?

    >
    > You misunderstand what "flush" means. It is not about clearing the
    > screen, or the line.
    >
    > Try printing
    >
    >    stdout.write('\r-->%d')
    >
    > Diez



    But there is still a problem. When you use control character '\r', you
    actually move to the head of the current buffer line and overwrite it.
    So if I use this way:
    for i in range(100, 0,-1)

    The tail of the buffer is not overwrote.

    How to solve this problem?

    Thanks.
    Jankins, Nov 24, 2009
    #4

  5. >> ....
    >> You misunderstand what "flush" means. It is not about
    >> clearing the screen, or the line.
    >>
    >> Try printing
    >>
    >>    stdout.write('\r-->%d')
    >>
    >> Diez

    >
    >
    > But there is still a problem. When you use control character '\r',
    > you actually move to the head of the current buffer line and
    > overwrite it.
    >
    > So if I use this way:
    > for i in range(100, 0,-1)
    >
    > The tail of the buffer is not overwrote.
    > ....


    The following version works ok for me
    using python2.5 under debian linux ....

    import sys
    import time

    print

    for n in range( 11 ) :
    sys.stdout.write( '\r Working ----> %d ' % n )
    sys.stdout.flush()
    time.sleep( 1 )

    else :
    print "\n"
    print " That's all, folks !"
    print " Adios ........... "


    --
    Stanley C. Kitching
    Human Being
    Phoenix, Arizona
    Cousin Stanley, Nov 24, 2009
    #5
  6. Jankins

    Dave Angel Guest

    Jankins wrote:
    > On Nov 23, 4:08 pm, "Diez B. Roggisch" <> wrote:
    >
    >> Jankins schrieb:
    >>
    >>
    >>
    >>
    >>
    >>
    >>> I am trying to use sys.stdout to print out "process-bar" like:
    >>> -->1%
    >>>
    >>> Here is my program ‘test.py’:
    >>>
    >>> from sys import stdout
    >>> for v in range(10):
    >>> stdout.write('-->%d' % v)
    >>> stdout.flush()
    >>> else:
    >>> stdout.write('done!')
    >>> #end for
    >>>
    >>> Then, I use 'python -u test.py' to run this script. But what I get
    >>> is :
    >>> -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!
    >>>
    >>> I am suppose to get 'done!'.
    >>>
    >>> Can anybody help me about this?
    >>>

    >> You misunderstand what "flush" means. It is not about clearing the
    >> screen, or the line.
    >>
    >> Try printing
    >>
    >> stdout.write('\r-->%d')
    >>
    >> Diez
    >>

    >
    >
    > But there is still a problem. When you use control character '\r', you
    > actually move to the head of the current buffer line and overwrite it.
    > So if I use this way:
    > for i in range(100, 0,-1)
    >
    > The tail of the buffer is not overwrote.
    >
    > How to solve this problem?
    >
    > Thanks.
    >
    >

    No idea what you mean by "buffer line." This is moving the cursor
    around on the console.

    Anyway, for such a loop, just make sure all the strings are the same
    length. Or just cheat and always write a few blanks at the end.

    sys.stdout.write("\r -- %5d" % i)

    should do it, for up to 5 digit values

    DaveA
    Dave Angel, Nov 24, 2009
    #6
  7. Jankins

    Jankins Guest

    On Nov 23, 8:32 pm, Cousin Stanley <> wrote:
    > >> ....
    > >> You misunderstand what "flush" means. It is not about
    > >> clearing the screen, or the line.

    >
    > >> Try printing

    >
    > >>    stdout.write('\r-->%d')

    >
    > >> Diez

    >
    > > But there is still a problem. When you use control character '\r',
    > > you actually move to the head of the current buffer line and
    > > overwrite it.

    >
    > > So if I use this way:
    > > for i in range(100, 0,-1)

    >
    > > The tail of the buffer is not overwrote.
    > > ....

    >
    >   The following version works ok for me
    >   using python2.5 under debian linux ....
    >
    > import sys
    > import time
    >
    > print
    >
    > for n in range( 11 ) :
    >     sys.stdout.write( '\r    Working ----> %d ' % n )
    >     sys.stdout.flush()
    >     time.sleep( 1 )
    >
    > else :
    >     print "\n"
    >     print "    That's all, folks !"
    >     print "    Adios ........... "
    >
    > --
    > Stanley C. Kitching
    > Human Being
    > Phoenix, Arizona


    Thanks. It works. Put some space at the end of the output string.
    Jankins, Nov 24, 2009
    #7
  8. Jankins

    Jankins Guest

    On Nov 23, 8:54 pm, Dave Angel <> wrote:
    > Jankins wrote:
    > > On Nov 23, 4:08 pm, "Diez B. Roggisch" <> wrote:

    >
    > >> Jankins schrieb:

    >
    > >>> I am trying to use sys.stdout to print out "process-bar" like:
    > >>> -->1%

    >
    > >>> Here is my program ‘test.py’:

    >
    > >>> from sys import stdout
    > >>> for v in range(10):
    > >>>     stdout.write('-->%d' % v)
    > >>>     stdout.flush()
    > >>> else:
    > >>>     stdout.write('done!')
    > >>> #end for

    >
    > >>> Then, I use 'python -u test.py' to run this script. But what I get
    > >>> is :
    > >>> -->0-->1-->2-->3-->4-->5-->6-->7-->8-->9done!

    >
    > >>> I am suppose to get 'done!'.

    >
    > >>> Can anybody help me about this?

    >
    > >> You misunderstand what "flush" means. It is not about clearing the
    > >> screen, or the line.

    >
    > >> Try printing

    >
    > >>    stdout.write('\r-->%d')

    >
    > >> Diez

    >
    > > But there is still a problem. When you use control character '\r', you
    > > actually move to the head of the current buffer line and overwrite it.
    > > So if I use this way:
    > > for i in range(100, 0,-1)

    >
    > > The tail of the buffer is not overwrote.

    >
    > > How to solve this problem?

    >
    > > Thanks.

    >
    > No idea what you mean by "buffer line."  This is moving the cursor
    > around on the console.
    >
    > Anyway, for such a loop, just make sure all the strings are the same
    > length.  Or just cheat and always write a few blanks at the end.
    >
    >        sys.stdout.write("\r -- %5d" % i)
    >
    > should do it, for up to 5 digit values
    >
    > DaveA


    '%5d' is elegant. I prefer adding some space at the end of the output
    string.
    Thanks.
    Jankins, Nov 24, 2009
    #8
  9. Jankins

    Nobody Guest

    On Mon, 23 Nov 2009 23:08:25 +0100, Diez B. Roggisch wrote:

    > Try printing
    >
    > stdout.write('\r-->%d')


    ^M-->0^M-->1^M-->2^M-->3... ;)

    But it's probably good enough for the OP's purposes.
    Nobody, Nov 24, 2009
    #9
    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:
    654
    Roman Neuhauser
    Apr 4, 2005
  2. Replies:
    2
    Views:
    649
    velle
    Jan 5, 2006
  3. Elad
    Replies:
    0
    Views:
    410
  4. Thomas Guettler

    stdout not flushed before os.execvp()

    Thomas Guettler, Oct 17, 2006, in forum: Python
    Replies:
    3
    Views:
    461
    Fulvio
    Oct 18, 2006
  5. Replies:
    0
    Views:
    466
Loading...

Share This Page