0

Initially I was using the following line in my script (python 3):

print ('{0}\t{1:.2f}\t{2:.8f}\t{3}'.format(var1, var2, var3, var4), end='\r')

The output gets updated in the same line. This is exactly the behavior that I want.

However due to some issues in a library that I'm using, I've changed my code to python 2. I used the following import statement: from __future__ import print_function at the top of the file. But it is not printing anything in my terminal. I have tried tinkering with the print statement but it always ends up printing in a new line.

Can someone please tell me the exact equivalent for the above statement in python 2? (Using from __future__ import print_function is allowed).

Update: The first two lines of my code are:

# -*- coding: utf-8 -*-
from __future__ import print_function

There is no line before these two. One of the variables in the print statement contains the character. I do not know if that could be the cause of the problem.


Update 2: As pointed out by https://stackoverflow.com/users/1639625/tobias-k in the comments, the behaviour of the print function changed with Python 3.3 and that change was not backported. Hence, we can't get it to work in python 2. An alternate way to do the same is given in the linked question using stdout.flush and stdout.write.

aste123
  • 1,223
  • 4
  • 20
  • 40
  • Do you need that carriage return? – cs95 Dec 12 '17 at 12:32
  • @cᴏʟᴅsᴘᴇᴇᴅ No. I just need the output to replace the older output on the same line. Using carriage return isn't needed for any other purpose in my application. – aste123 Dec 12 '17 at 12:33
  • @cᴏʟᴅsᴘᴇᴇᴅ OP wants to print in the same line so that the text appears to update itself. Maybe `flush` \ `buffering` settings are different? – Ma0 Dec 12 '17 at 12:34
  • Did you, by any chance, switch operating systems as well? – cs95 Dec 12 '17 at 12:34
  • @cᴏʟᴅsᴘᴇᴇᴅ I'm writing the code using sublime text on windows 10 and compiling using bash (windows 10 comes with it). I do not have python in windows 10 right now. So, the compilation must have been done in the same compiler each time. – aste123 Dec 12 '17 at 12:37
  • @cᴏʟᴅsᴘᴇᴇᴅ I have updated the question with first two lines of code. – aste123 Dec 12 '17 at 12:41
  • Did that actually do what you wanted in Python 3 (can't see how it could have)? Are you sure you weren't writing to stderr instead or had otherwise unbuffered the stream? – Jon Clements Dec 12 '17 at 12:41
  • @JonClements I have added a screenshot from my server which was running this code with python 3 perfectly. – aste123 Dec 12 '17 at 12:47
  • Err... what does that show apart from what you've already put in your question? Smells like differences between environments regarding line endings or buffering... – Jon Clements Dec 12 '17 at 12:50
  • @JonClements I have run the same python 3 script in my current environment and it works. – aste123 Dec 12 '17 at 12:51
  • I would recommend placing the carriage return *at the beginning* of the line, i.e. `print('\r…', end='')` – poke Dec 12 '17 at 12:56
  • 1
    Seems like it does behave differently in Python 2 without `flush`, and the `flush` parameter is not available, at least not in 2.7. You can still use `sys.stdout` or even `curses`, see e.g. [here](https://stackoverflow.com/q/26584003/1639625) – tobias_k Dec 12 '17 at 12:58
  • Also related: https://stackoverflow.com/q/230751/1639625 – tobias_k Dec 12 '17 at 13:01
  • @poke When I try your suggestion, it doesn't print anything at all (or maybe overwrites itself but I do not see anything on terminal). The print statement is within a loop as stated in question with a time.sleep(t) at the end of each iteration. I tried `print ('\r{0}\t{1:.2f}\t{2:.8f}\t{3}'.format(a, b, c, d), end='')` – aste123 Dec 12 '17 at 13:07
  • @tobias_k I do not want to flush everything that is on screen. There are statements earlier that have to be kept. I only want to overwrite the print statement in the question each time it is executed. Can you please give an example of how to do it using other ways like flush, etc? – aste123 Dec 12 '17 at 13:09
  • 1
    "flush" does not mean "clear screen", much the contrary: It means "write everything to the screen that's in the buffers". Without it, Python might decide to write the line only when the line is finished, which in you case will never be the case. Try one of the linked answers. – tobias_k Dec 12 '17 at 13:13
  • @tobias_k Finally, able to do it using write() and flush(). Thank you so much :) – aste123 Dec 12 '17 at 13:17
  • @tobias_k It's definitely not a duplicate. The other solution is an alternate solution which works for my case however, the question is about why the python 3 statement isn't working in python 2 after importing `print_function` from `__future__`. Someone looking for this may not benefit from the linked question. A duplicate would be one where solution is given using `print()` function. But I'm not able to find such a solution on the site which works. – aste123 Dec 12 '17 at 15:13
  • Well, I guess that was mentioned in comments somewhere: That the behaviour of the `print` function changed with Python 3.3 and that change was not backported. – tobias_k Dec 12 '17 at 15:18
  • @tobias_k Ok, it should be fine then. – aste123 Dec 12 '17 at 15:20

1 Answers1

0

end='\r' causes the output to be overwritten by your shell prompt as soon as Python finishes.

tripleee
  • 175,061
  • 34
  • 275
  • 318