0

In the following code, I would like to show the progress of two loops with the iteration number for the outer loop and dots for the inner loop.

import time
def foo():
    for j in range(0,100):
        if j % 10 == 0:
            print('.', end='', flush=True, sep='')
            time.sleep(0.2)

for i in range(0,100):
    if i % 10 == 0:
        print('\r', i, end='', flush=True, sep='')
        time.sleep(1)
    foo()

Although at i==10 it goes to the beginning of the line, it doesn't remove the dots. So, it actually overwrites the dots. See this figure:

enter image description here

Is there a way to clean the line and go to the start point with \r?

mahmood
  • 23,197
  • 49
  • 147
  • 242
  • 1
    Add some spaces to overwrite: `print('\r \r', i, end='', flush=True, sep='')`. Or use [ansi escape sequence](https://en.wikipedia.org/wiki/ANSI_escape_code#CSI_(Control_Sequence_Introducer)_sequences): `print("\r\u001b[0K", end="")`. – 001 Jan 18 '23 at 18:19
  • Note: the escape sequences don't work in all terminals - especially online psuedo-terminals. Also, to enable them in Windows console see: [Python: How can I make the ANSI escape codes to work also in Windows?](https://stackoverflow.com/q/12492810) – 001 Jan 18 '23 at 18:25

0 Answers0