I just had this problem. You can still use \r
, even in Windows Command Prompt, however, it only takes you back to the previous linebreak (\n
).
If you do something like this:
cnt = 0
print str(cnt)
while True:
cnt += 1
print "\r" + str(cnt)
You'll get:
0
1
2
3
4
5
...
That's because \r
only goes back to the last line. Since you already wrote a newline character with the last print statement, your cursor goes from the beginning of a new empty line to the beginning of the same new empty line.
To illustrate, after you print the first 0, your cursor would be here:
0
| # <-- Cursor
When you \r
, you go to the beginning of the line. But you're already on the beginning of the line.
The fix is to avoid printing a \n
character, so your cursor is on the same line and \r
overwrites the text properly. You can do that with print 'text',
. The comma prevents the printing of a newline character.
cnt = 0
print str(cnt),
while True:
cnt += 1
print "\r" + str(cnt),
Now it will properly rewrite lines.
Note that this is Python 2.7, hence the print
statements.