I am writing with python 2.7
I have the following code:
a = 0
b = 0
while True:
a += 1
b += 1
print str(a)
print str(b)
The output looks like:
1
1
2
2
3
3
4
....
And want to flush these two lines with stdout.flush()
. The code looks like this but it's not working..
import sys
a = 0
b = 0
while True:
a += 1
b += 1
sys.stdout.write(str(a)+"\n")
sys.stdout.write(str(b)+"\r")
sys.stdout.flush()
And this produces an output like this:
1 #1
2 #1 ->#2
3 #2 ->#3
4 #3 ->#4
...
I know this is because \r
only jump to the beginning of the 2nd line, and start from the the next prints..
How can I set the cursor to the beginning of 1st line instead of the 2nd line?
So that it will refresh only 2 lines:
n #1 ->#2 ->#3 ->#4 ->#.....
n #1 ->#2 ->#3 ->#4 ->#.....
I hope somebody will understand what I mean.