0

I have two functions prints() and clear(). The function prints() print lines of text and the function clear() delete the printed lines with this method:

def clear():
    i = 0
    while True:
        if(i > globals['l']):
            break
        else:
            sys.stdout.write("\033[F \033[K")
            i += 1

where globals['l'] is the number of lines to clear.

Then after the function clear() runs and the lines are cleared, the function prints() run again etc...

I don't understand why the function clear() is clearing only 22 lines of 32 lines. But if I have, for example, 19 lines it is working perfectly. Where is the problem? How can I fix this?

2
  • As an aside, this could be a one-liner: says.stdout.write("\033[F \033[K" * l) Commented Jan 6, 2017 at 17:36
  • Thanks for the tip, but the problem is still there Commented Jan 6, 2017 at 17:45

2 Answers 2

2

Try this:

def clear(line_count):
    sys.stdout.write("\033[F \033[K" * line_count)
    sys.stdout.flush()
Sign up to request clarification or add additional context in comments.

Comments

0

EDIT: Works only on Unix systems

You can use:

print("\033c")

Or:

import subprocess
subprocess.call(["printf", "\033c"])

1 Comment

it works only on Unix systems, I tried several alternatives for windows, but I haven't found the solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.