1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] How to clear only last one line in python output console?

Discussão em 'Python' iniciado por Stack, Novembro 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to clear only last few line from output console window. To achieve this I have decided to use create stopwatch and I have achieved to interrupt on keyboard interrupt and on enter key press it creates lap but my code only create lap once and my current code is clearing whole output screen.

    clear.py

    import os
    import msvcrt, time
    from datetime import datetime
    from threading import Thread

    def threaded_function(arg):
    while True:
    input()

    lap_count = 0
    if __name__ == "__main__":
    # thread = Thread(target = threaded_function)
    # thread.start()
    try:
    while True:
    t = "{}:{}:{}:{}".format(datetime.now().hour, datetime.now().minute, datetime.now().second, datetime.now().microsecond)
    print(t)
    time.sleep(0.2)
    os.system('cls||clear') # I want some way to clear only previous line instead of clearing whole console
    if lap_count == 0:
    if msvcrt.kbhit():
    if msvcrt.getwche() == '\r': # this creates lap only once when I press "Enter" key
    lap_count += 1
    print("lap : {}".format(t))
    time.sleep(1)
    continue
    except KeyboardInterrupt:
    print("lap stop at : {}".format(t))
    print(lap_count)


    when I run

    %run <path-to-script>/clear.py


    in my ipython shell I am able to create only one lap but it is not staying for permanent.

    Continue reading...

Compartilhe esta Página