0

Can someone please thoroughly explain how a '\r' works in Python? Why isn't the following code printing out anything on the screen?

#!/usr/bin/python

from time import sleep

for x in range(10000):
    print "%d\r" % x,
    sleep(1)

4 Answers4

2

Your output is being buffered, so it doesn't show up immediately. By the time it does, it's being clobbered by the shell or interpreter prompt.

Solve this by flushing each time you print:

#!/usr/bin/python

from time import sleep
import sys

for x in range(10000):
    print "%d\r" % x,
    sys.stdout.flush()
    sleep(1)
Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • Excuse my ignorance, what is flushing? –  Mar 21 '14 at 17:45
  • To improve performance, output is often buffered. That means that it gets stored up in memory until the buffer is full or it gets flushed (actually sent to output). `\n` will usually flush the buffer implicitly, but you're not printing one. So you need to ask for an explicit flush. – Fred Larson Mar 21 '14 at 17:47
  • also, I am not quite sure how a '\r' and a comma after a print statement work. Could you also explain that? –  Mar 21 '14 at 17:47
  • 2
    `\r` is a carriage return character. As others have said, its behavior depends on what kind of terminal you're using. But many (perhaps most) will move the cursor to the beginning of the current line without creating a new line. Python's `print` statement normally puts a newline on the end of it's output, but a trailing comma suppresses that. – Fred Larson Mar 21 '14 at 17:51
1

'\r' is just a another ASCII code character. By definition it is a CR or carriage return. It's the terminal or console being used that will determine how to interpret it. Windows and DOS systems usually expect every line to end in CR/LF ('\r\n') while Linux systems are usually just LF ('\n'), classic Mac was just CR ('\r'); but even on these individual systems you can usually tell your terminal emulator how to interpret CR and LF characters.

Historically (as a typewriter worked), LF bumped the cursor to the next line and CR brought it back to the first column.

To answer the question about why nothing is printing: remove the comma at the end of your print line.

Octopus
  • 8,075
  • 5
  • 46
  • 66
1

Do this instead:

print "\r%d" % x,
Eugene C.
  • 495
  • 4
  • 13
0

This has nothing to do with \r. The problem is the trailing , in your print statement. It's trying to print the last value on the line, and the , is creating a tuple where the last value is empty. Lose the , and it'll work as intended.

Edit:

I'm not sure it's actually correct to say that it's creating a tuple, but either way that's the source of your problem.

Jim Stewart
  • 16,964
  • 5
  • 69
  • 89
  • 1
    It's printing on separate lines now. –  Mar 21 '14 at 17:20
  • Interpretation of a `\r` is outside the scope of Python. Your terminal emulator decides what to do. If you're trying to overwrite the same line, and you're seeing multiple lines, that's your terminal at work. If you want to overwrite the same line in a cross-terminal way, you'll need to uses the [curses library](http://docs.python.org/2/library/curses.html). – Jim Stewart Mar 21 '14 at 17:21
  • `\r` and the trailing comma works just fine for me if I add a flush after each print. – Fred Larson Mar 21 '14 at 17:31
  • 1
    `,` at the end of `print` statement in Python 2.x skips `\n` which is implicitly printed otherwise. – Fenikso Mar 21 '14 at 17:32