help with printing to stdout...

2009-03-08 Thread Daniel Dalton
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
every time the line print percent is ran it should delete the old
percentage from the screen, replacing it with the new one, so as to only
use up one line... Basically I'm just printing a string of text to the
screen and every time my print command is ran I would like the old text
to be removed and my new text added (talking about the one line of the
screen here)... This is a command line program, under linux...

Thanks,

Daniel.
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with printing to stdout...

2009-03-08 Thread Chris Rebert
On Sun, Mar 8, 2009 at 1:37 AM, Daniel Dalton d.dal...@iinet.net.au 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with printing to stdout...

2009-03-08 Thread D'Arcy J.M. Cain
On Sun, 8 Mar 2009 01:59:03 -0800
Chris Rebert c...@rebertia.com wrote:
  etc taking up lots and lots of lines of output... So, how can I make it
  write the percentage on the same line eg.
 Use the carriage return character to overwrite the line (you'll need
 to forego `print`):

Why do you say that?

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

while working:
  print '\r'+percent,

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

Nope.  You just need to make sure that it never gets shorter which is
the case here.  If it wasn't then:

while working:
  print '\r'+percent+' ',

-- 
D'Arcy J.M. Cain da...@druid.net |  Democracy is three wolves
http://www.druid.net/darcy/|  and a sheep voting on
+1 416 425 1212 (DoD#0082)(eNTP)   |  what's for dinner.
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with printing to stdout...

2009-03-08 Thread Hendrik van Rooyen
Daniel Dalton d.dal...@iinet.net.au wrote:

 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
 every time the line print percent is ran it should delete the old
 percentage from the screen, replacing it with the new one, so as to only
 use up one line... Basically I'm just printing a string of text to the
 screen and every time my print command is ran I would like the old text
 to be removed and my new text added (talking about the one line of the
 screen here)... This is a command line program, under linux...

Play with the following:

put a comma after the print, like this:

print percent,#This keeps it on the same line

Then put a carriage return at the start, like this:

print '\r',percent,

or like this:

print '\r'+str(percent),

Then make sure it gets sent out, like this:

sys.stdout.flush()

Alternatively, you can play with backspaces instead of the carriage return:

print '\b\b\b\b',
print percent,
sys.stdout.flush()

And see what happens.

- Hendrik


--
http://mail.python.org/mailman/listinfo/python-list


Re: help with printing to stdout...

2009-03-08 Thread Lie Ryan

Chris Rebert wrote:

On Sun, Mar 8, 2009 at 1:37 AM, Daniel Dalton d.dal...@iinet.net.au 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
--
http://mail.python.org/mailman/listinfo/python-list


Re: help with printing to stdout...

2009-03-08 Thread Daniel Dalton
 or like this:
 
 print '\r'+str(percent),
 
 Then make sure it gets sent out, like this:
 
 sys.stdout.flush()

Hey!

Thanks very much, that did the trick!

Thanks to everyone that replied, I discovered converting to str was
crutial to actually print anything. :)

Cheers,

Daniel.
--
http://mail.python.org/mailman/listinfo/python-list