I want to know how properly solve a problem similar to this:
With one exception, my script is using a new line character, and it looks like this:
#!/usr/bin/env python
import time
import sys
import random
total = 100
progress = 0
for i in xrange(0, 100):
time.sleep(0.01)
progress += 1
random_word = 'a' * random.randint(1, 10)
random_int = random.randint(4, 12)
how_many_characters = 99
sys.stdout.write("%d/100 %s\n" % (progress, random_word * random_int))
sys.stdout.write("\b" * how_many_characters)
While this line
sys.stdout.write("\b" * how_many_characters)
is the problem.
How to determine where previous line starts and how long is it? Or how to move to first character of previous line? Or what else I can do? What I want to achieve is to change already displayed text
1/100 Crazy white fox jumping over fence\n
Into:
2/100 Crazy white fox jumping over fence\n
Extra requirements:
- I cannot remove that trailing \n from original sentence
- I know that progress bar is displayed at beginning of previous line
Edit.
Improved question to remove ambiguity.