1

I'm pretty new with Python, but I am trying to do something which I believe is a pretty simple thing. Nevertheless it has been bothering me for quite a while.

I have a code that does a certain number of iterations and I want to print, on the same line, a status of the progress.

I tried the following

y=1000000
for x in range(y):
    if x % 100000 == 0 and x!=0 or x==y :
        print "  Iteration %d out of %d\r" % (x,y)

but what I get is not a carriage return but simply

  Iteration 100000 out of 1000000
  Iteration 200000 out of 1000000
  Iteration 300000 out of 1000000
  Iteration 400000 out of 1000000
  Iteration 500000 out of 1000000
  ...

printed out at video.

The funny thing is that if I do

for x in range(1000000):
    print "%d\r" % x,

it does the job. Does anybody knows why?

martineau
  • 119,623
  • 25
  • 170
  • 301
dani8586
  • 21
  • 1
  • 2

3 Answers3

3

In Python 2.x

To suppress printing newline, add a trailing comma:

print s ,

So in this case:

print "  Iteration %d out of %d\r" % (x,y) ,

(Tip: I always leave a space before the trailing comma to make it clear)

In Python 3.x

print x,           # 2.x: Trailing comma suppresses newline
print(x, end="")   # 3.x: Appends a space instead of a newline
Community
  • 1
  • 1
smci
  • 32,567
  • 20
  • 113
  • 146
  • 2
    Hi, unfortunately id doesn't work... what I get is no print at terminal during the loop and just a final print which is wrong, i.e – dani8586 Oct 24 '16 at 07:42
  • 0 out of 10000000 – dani8586 Oct 24 '16 at 07:42
  • It **does** work for me, and I get *"Iteration 100000 out of 1000000... Iteration 200000 out of 1000000... Iteration 900000 out of 1000000"* Look, you asked about suppressing newlines in printing and this was the solution to that. As to why your loop doesn't behave for you as it does for the rest of us, check your indentation, sprinkle in more print statements, try parenthesizing the conditional subexpressions, or even printing them if needs. If you can't solve it yourself, repost it as a new question, including the exact code as you indented it. We can't diagnose without seeing code. – smci Oct 24 '16 at 09:55
  • Anyway, this question was already closed(!) because the original "suppress newline in printing" was a duplicate. Open a new question if you need to. – smci Oct 24 '16 at 10:08
  • 1
    I see. I just reported that it doesn't work for me, since I believe it could be a useful information also for other people reading this topic. The code is pretty simple, i just substitute the last line of my previous one with the one you suggested, nothing too complicated. Yet it doesn't do the job. Sorry about the duplicate question, but I believe I should be written that this solution doesn't work for me, since it might happen for other people. – dani8586 Oct 24 '16 at 13:36
  • 1
    @dani8586 I encouter a similar issue, and in my case it is a buffering issue. There are two solutions for that issue : launch python in unbuffered mode using `python -u` or call `sys.stdout.flush()` after each print; see that [so question](http://stackoverflow.com/questions/230751/how-to-flush-output-of-python-print). – jadsq Oct 24 '16 at 21:51
  • Did you mean the code was buffered, and prints all the missing lines all at the end? or that it genuinely never prints them? Did you confirm by running unbuffered mode as @jadsq show? If so, I still suspect your indentation like I said above, but since you didn't post the exact code as written we can't know. If it still happens, tell us your OS version (Windows? which?). I suspect if you run on another OS it will not happen. Please check all of that. – smci Oct 25 '16 at 19:54
  • with python -u I actually see it. Also, I manage to make it work in this way (complete code) import sys y=10000000 for x in range(y): if x % 1000000 == 0 and x!=0 or x==y : sys.stdout.write("\rIteration " + str(x) + " out of " + str(y)) sys.stdout.flush() My OS is a mac 10.11.6 – dani8586 Oct 26 '16 at 11:13
1

This is only for python 3. You can add end="" and this will make sure that the output is printed on the same line. print print will write the output to the console on a new line.

The print function accepts an end parameter which defaults to "\n". Setting it to an empty string prevents it from issuing a new line at the end of the line.

Saad
  • 399
  • 8
  • 25
1

Instead of using print, you can also use

sys.stdout.write("  Iteration %d out of %d\r" % (x,y))

This doesn't add any newlines to what you write.

Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • this doesn't work either... I get no print during the loop and just 0 out of 10000000 at the end... – dani8586 Oct 24 '16 at 07:43
  • This needs `sys.stdout.flush()` after each call to `write`. – valid Aug 22 '17 at 11:47
  • When used with `bash` you may want to append `\033[K`, e.g. `" Iteration %d out of %d\r\033[K"`, to clear the remainder of the line and any artifacts on it that were not overwritten by the last `write`. – valid Aug 22 '17 at 13:01