tags:

views:

85

answers:

3

Basically I want to do the opposite of what this guy did... hehe.

http://stackoverflow.com/questions/529395/python-script-print-new-line-each-time-to-shell-rather-than-update-existing-line

I have a program that is telling me how far along it is.

for i in some_list:
    #do a bunch of stuff.
    print i/len(some_list)*100," percent complete"

So if len(some_list) was 50, I'd get that last line printed 50 times over. I want to print one line and keep updating that line. I know I know this is probably the lamest question you'll read all day. I just can't figure out the four words I need to put into google to get the answer.

Update! I tried mvds' suggestion which SEEMED right. The new code

print percent_complete,"           \r",

Percent complete is just a string (I was abstracting the first time now I an trying to be literal). The result now is that it runs the program, doesn't print ANYTHING until after the program is over, and then prints "100 percent complete" on one and only one line.

Without the carriage return (but with the comma, half of mvds' suggestion) it prints nothing until the end. And then prints:

0 percent complete     2 percent complete     3 percent complete     4 percent complete    

And so on. So now the new issue is that with the comma it doesn't print until the program is finished.

With the carriage return and no comma it behaves the exact same as with neither.

+7  A: 

It's called the carriage return, or \r

Use

print i/len(some_list)*100," percent complete         \r",

The comma prevents print from adding a newline. (and the spaces will keep the line clear from prior output)

Also, don't forget to terminate with a print "" to get at least a finalizing newline!

mvds
Just make sure you're always printing the same amount of data (or more than any previous print) to the line, otherwise you'll end up with cruft at the end.
Nicholas Knight
+1 really good point, will fix
mvds
So close... I'll update the question with the result of this.
dustynachos
@dustynachos: Heh, forgot about that wrinkle. See the Python Output Buffering question: http://stackoverflow.com/questions/107705/python-output-buffering
Nicholas Knight
@dustynachos: (or just use sys.stdout.flush() after each print call, that may actually be better if you don't care about output buffering for the rest of your program)
Nicholas Knight
A: 

Try it like this:

for i in some_list:
    #do a bunch of stuff.
    print i/len(some_list)*100," percent complete",

(With a comma at the end.)

chryss
This just appends the new text to the old (functionally similar but ugly).
dustynachos
A: 

This works for me, hacked it once to see if it is possible, but never actually used in my program (GUI is so much nicer):

import time
f = '%4i %%'
len_to_clear = len(f)+1
clear = '\x08'* len_to_clear
print 'Progress in percent:'+' '*(len_to_clear),
for i in range(123):
    print clear+f % (i*100//123),
    time.sleep(0.4)
raw_input('\nDone')
Tony Veijalainen