1

I have written a small tool that iterates over a wordlist and I want it to print each line from the wordlist over the previous line, I have tried the following but it has issues.

print(word + (100 * " "), end="\r", flush=True)

This works as expected in VSCodes in-built terminal but when I run the same tool in either the OS's (Xfce) in-built terminal or Terminator each "word" is printing on a newline

Does anyone know why this is happening all my research just points me on how to do it, which doesn't help as the way to do it is what has this issue.

Thanks

akaBase
  • 2,038
  • 1
  • 13
  • 26
  • Have you look on stackoverflow? https://stackoverflow.com/questions/4897359/output-to-the-same-line-overwriting-previous-output – GabrielC May 19 '20 at 15:51
  • I had and like my question says, they point to the method I'm using that example just doesn't have the addition of `flush=True` – akaBase May 19 '20 at 15:54

1 Answers1

2

This is because depending on your window width (and word length), each string may be longer than the line. So the next print statement is 'overwriting' the previous string, but since that previous string jumped a line, so the new string starts on that new line.

You can see this in action by printing 99 spaces followed by a * or some other character:

print(word + (99 * " ") + "*")

Depending on your window width and word length, this may or may not jump a line, as demonstrated by where there "*" prints.

SimonR
  • 1,644
  • 1
  • 2
  • 10
  • Thanks this makes a lot of sense will test but have a sneaking suspicion you are correct! – akaBase May 19 '20 at 15:55
  • Thanks, I was adding to much padding to overwrite anything longer than the new line, calculate it now :) – akaBase May 19 '20 at 16:01