1

So I am trying to use sys.stdout.write() to rewrite the current line it is on and replace it with different text. But I want this to happen for multiple lines of output.

  # I am looking at having 2 lines with changing values in them that will keep over writing themselves without moving any further down.
  import sys
  for i in range(10):
       sys.stdout.write("\r" + "Hello" + str(i) + "\n + "Hello" + str(i) * 2)
       sys.stdout.flush()

I tried this but every time it hits the \n it will indefinately increase the numbers of lines without clearing everything.

halfer
  • 19,824
  • 17
  • 99
  • 186
RoNAPC
  • 155
  • 2
  • 9
  • (Don't worry about marking your post as "not urgent" - nothing is urgent here anyway, so I've trimmed it). – halfer Feb 02 '17 at 14:09

1 Answers1

2

stdout\stderr don't work like that. You can send control characters to your terminal to delete characters but once a line is sent it's basically fixed.

What you actually need is ncurses which gives you much more control over the display and cursor position when writing to a terminal.

SpliFF
  • 38,186
  • 16
  • 91
  • 120