0

I need to print a couple of error messasges to the screen and then erase them without disturbing the subsequent output to the user. I'm using ANSI Escape Sequences for cursor control. The code works if I print the error messages to sys.stdout but fails if sys.stderr is used.

Here is a sample code:

import sys, os
import time
os.system("")

errors = ["Error1", "Error2"]

for e in errors:
    print(e, file=sys.stderr)
    time.sleep(1)
    print("\033[F\033[2K\033[1G", file=sys.stderr, end='')
    # print("\033[F\033[2K\033[1G", end='')  # This gives desired result

print("Standard output")

I expected the screen to display each error message for a second and then erase them. At the end, the screen should just display "Standard output". Testing the code on Python 3.11, the screen actually shows "Error2" at the end.

Note: Changing the file=sys.stderr to file=sys.stdout yields desired result but I was hoping to display errors on a separate channel from the usual output.

Elvis M
  • 13
  • 4

0 Answers0