0

How can I delete a complete line in the output screen of python?

Can I use the escape sequence '\b' for this?

  • maybe relevant? https://stackoverflow.com/questions/5419389/how-to-overwrite-the-previous-print-to-stdout-in-python – user9423368 Mar 05 '18 at 11:42

1 Answers1

1

What your asking is somewhat terminal-specific. However, the following solution should work in both Linux and Windows.

  1. Write \r to return to the beginning of the current line.

  2. Write as many spaces as needed to "cover" any previous content on the line.

  3. Write \r to return to the beginning of the current line again.

  4. Write the new text for this line.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
  • Thanks for your answer, can you please explain step 2 in detail? – ashutosh_paul Mar 05 '18 at 13:22
  • @user8718189 That part is left as an exercise to the reader. It depends on your application requirements. Either you need to write a wrapper function that keeps track of how many characters you've written, or just always write some amount like `80`. – Jonathon Reinhart Mar 05 '18 at 14:17