Re: help with printing to stdout...

Discussion in 'Python' started by Chris Rebert, Mar 8, 2009.

  1. Chris Rebert

    Chris Rebert Guest

    On Sun, Mar 8, 2009 at 1:37 AM, Daniel Dalton <> wrote:
    > Hi,
    >
    > I've got a program here that prints out a percentage of it's
    > completion. Currently with my implimentation it prints like this:
    > 0%
    > 1%
    > 2%
    > 3%
    > 4%
    >
    > etc taking up lots and lots of lines of output... So, how can I make it
    > write the percentage on the same line eg.
    > while working:
    >  print percent


    Use the carriage return character to overwrite the line (you'll need
    to forego `print`):

    from sys import stdout
    while working:
    stdout.write('\r'+percent)

    Note that you'll need to ensure that `percent` has constant length
    throughout the loop.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
    Chris Rebert, Mar 8, 2009
    #1
    1. Advertising

  2. Chris Rebert

    Lie Ryan Guest

    Chris Rebert wrote:
    > On Sun, Mar 8, 2009 at 1:37 AM, Daniel Dalton <> wrote:
    >> Hi,
    >>
    >> I've got a program here that prints out a percentage of it's
    >> completion. Currently with my implimentation it prints like this:
    >> 0%
    >> 1%
    >> 2%
    >> 3%
    >> 4%
    >>
    >> etc taking up lots and lots of lines of output... So, how can I make it
    >> write the percentage on the same line eg.
    >> while working:
    >> print percent

    >
    > Use the carriage return character to overwrite the line (you'll need
    > to forego `print`):
    >
    > from sys import stdout
    > while working:
    > stdout.write('\r'+percent)
    >
    > Note that you'll need to ensure that `percent` has constant length
    > throughout the loop.
    >


    or "erase" the previous character first with whitespace
    stdout.write('\r \r%s%%' % percent).

    curse might be more reliable for this kind of thing (though it is
    definitely an overkill)

    Note: \r doesn't work on IDLE
    Lie Ryan, Mar 8, 2009
    #2
    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. Jody Burgess
    Replies:
    2
    Views:
    548
    George Kinney
    Jul 17, 2004
  2. Elad
    Replies:
    0
    Views:
    410
  3. Daniel Dalton

    help with printing to stdout...

    Daniel Dalton, Mar 8, 2009, in forum: Python
    Replies:
    0
    Views:
    243
    Daniel Dalton
    Mar 8, 2009
  4. Andreas S
    Replies:
    3
    Views:
    261
    Eric Hodel
    Dec 9, 2006
  5. Replies:
    2
    Views:
    331
    A. Sinan Unur
    Dec 7, 2005
Loading...

Share This Page