1

I am using Python 2.7.13(in Kali 4.9.0 amd64), and I have tried every variation of the print() statement that I can find(with the proper future import statements), and I am ending up with OK on a newline everytime. Here is the code:

from __future__ import print_function

for i in range(0, 10000):
print("\rOK", end="")

I have tried:

  • single quotes instead of double for end=""

  • using Python 2.x syntax instead of importing print_function and using 3.0 syntax

  • end='\r' end="\r"

  • a space in between the quotes of end=""

When I used the 2.7 syntax, I was able to get the output to stay on the same line but still printing in a row, when I try to use a carriage return to return the cursor to the beginning position(and overwrite previous output), it inserts a newline, even with a terminating comma in my print statement. I have read Question 3249524, along with well over a dozen other explanations for this problem. I have also tried using the sys.stdout functions instead:

import sys

for i in range(10):
sys.stdout.write("\rCountdown: %d" % i)
sys.stdout.flush()

The output of both programs is still on a newline every time. I am at a loss as to how to get it to print in the same place on the same line. Thanks to anyone who can illuminate this issue.

Community
  • 1
  • 1
Ostküste
  • 23
  • 6
  • What shell/terminal manager are you using? They may not properly handle carriage returns. I can make this work on, for example bash on Ubuntu on Windows just fine, as long as I put a `time.sleep` in between each print so I can see the numbers change slowly. – ShadowRanger May 11 '17 at 22:07
  • ShadowRanger I was using Spyder but now I am running everything from bash directly and have completely uninstalled Spyder and stopped using it. The carriage return works exactly as it should when running the script from the command line, I suspect a lot of the people having the carriage return issue are running their scripts directly in their IDE console, and not from the command line. – Ostküste May 12 '17 at 14:50
  • That's exactly why I asked. I've heard of similar issues in a variety of custom shells (many of them integrated into IDEs). You also see issues with `multiprocessing` scripts in many cases. – ShadowRanger May 12 '17 at 22:53

0 Answers0