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. Advertisements

  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. Advertisements

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:
    692
    George Kinney
    Jul 17, 2004
  2. Elad
    Replies:
    0
    Views:
    590
  3. Pappy
    Replies:
    3
    Views:
    536
  4. Daniel Dalton

    help with printing to stdout...

    Daniel Dalton, Mar 8, 2009, in forum: Python
    Replies:
    0
    Views:
    308
    Daniel Dalton
    Mar 8, 2009
  5. Andreas S
    Replies:
    3
    Views:
    466
    Eric Hodel
    Dec 9, 2006
  6. Peter Vanderhaden

    Problem printing variable with $stdout.print

    Peter Vanderhaden, Nov 22, 2007, in forum: Ruby
    Replies:
    5
    Views:
    122
    Sebastian Hungerecker
    Nov 22, 2007
  7. Replies:
    2
    Views:
    609
    A. Sinan Unur
    Dec 7, 2005
  8. Hal Vaughan

    Printing to a file Or STDOUT

    Hal Vaughan, Aug 21, 2006, in forum: Perl Misc
    Replies:
    3
    Views:
    183
    Big and Blue
    Aug 22, 2006
Loading...