Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print to the same line and not a new line?

Tags:

python

stdout

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

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.

like image 360
chriscauley Avatar asked Aug 05 '10 23:08

chriscauley


People also ask

How do I keep print on the same line?

Modify print() method to print on the same line The print method takes an extra parameter end=” “ to keep the pointer on the same line. The end parameter can take certain values such as a space or some sign in the double quotes to separate the elements printed in the same line.

How do you stop going to the next line in Python?

It is used to indicate the end of a line of text. You can print strings without adding a new line with end = <character> , which <character> is the character that will be used to separate the lines.

How do I keep the print on the same line in Python?

To print on the same line in Python, add a second argument, end=' ', to the print() function call.

How do you print multiple lines on the same line?

To print multiple expressions to the same line, you can end the print statement in Python 2 with a comma ( , ). You can set the end argument to a whitespace character string to print to the same line in Python 3. With Python 3, you do have the added flexibility of changing the end argument to print on the same line.


2 Answers

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!

like image 106
mvds Avatar answered Sep 23 '22 08:09

mvds


From python 3.x you can do:

print('bla bla', end='') 

(which can also be used in Python 2.6 or 2.7 by putting from __future__ import print_function at the top of your script/module)

Python console progressbar example:

import time  # status generator def range_with_status(total):     """ iterate from 0 to total and show progress in console """     n=0     while n<total:         done = '#'*(n+1)         todo = '-'*(total-n-1)         s = '<{0}>'.format(done+todo)         if not todo:             s+='\n'                 if n>0:             s = '\r'+s         print(s, end='')         yield n         n+=1  # example for use of status generator for i in range_with_status(10):     time.sleep(0.1) 
like image 28
Remi Avatar answered Sep 24 '22 08:09

Remi