Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: Printing in the same exact spot using \r not working?

I am trying to print in the exact same spot:

for i in range(10):
    print("ok\r"),

However, this prints 10 oks in different lines:

ok
ok
...

I tried putting the \r in the front of the string as well. In this case, I get a blank line then 10 oks:

[blank]
ok
ok
...

I also tried the following

import sys

for i in range(10):
    sys.stdout.write('ok\r')
    sys.stdout.flush()

But I get the same exact thing. I am on Python 2.7 in pyscripter, what am I doing wrong? I also tried in IDLE, and there 'okokokokokokokokokok' is the result when \r is on the end, and ' ok ok ok ok ok ok ok ok ok ok' is the result when it is in the front.

Edit: I am on windows 7

Any help is highly appreciated!

like image 494
casandra Avatar asked Jul 24 '14 23:07

casandra


1 Answers

Try the following code, it works on ubuntu, I added a time.sleep call so you can see what is actually happening:

import time
for i in range(10):
    print(str(i),end='\r')
    time.sleep(.4)
like image 127
Padraic Cunningham Avatar answered Oct 27 '22 00:10

Padraic Cunningham