0

I am creating some code that updates your clock infinitely. I am using the datetime module currently.

import datetime as dt

def local_time():

    def time_check(t):
        if t < 10:
            t = "0{}".format(t)
        else:
            t = t
            
        return t

    p = dt.datetime.now()

    hour = p.hour
    minute = p.minute
    second = p.second 

    hour = time_check(hour)
    minute = time_check(minute)
    second = time_check(second)

    local_time = '{}:{}:{}'.format(hour, minute, second)
    print(local_time)

for i in range(9999999999999999999):
    local_time()
    delete_last_line_function()

The time_check() function is used to turn the hour, minute, and second into 24-hour time format. The problem I am having is that I am trying to delete the previous line, then fill it in with the new updated time. Obviously, delete_last_line_function() doesn't exist. I am using it as a place holder. Does anyone have any way of delete the previous line?

NotASuperSpy
  • 43
  • 2
  • 6
  • in bash use : history -d linenumber, which you can find with a history | grep "executed_command" – Charly Roch May 19 '21 at 08:43
  • Similar question here: https://stackoverflow.com/questions/44565704/how-to-clear-only-last-one-line-in-python-output-console and information about backspace (which might work) here https://stackoverflow.com/questions/48521783/backspace-in-python-3x – Pam May 19 '21 at 08:51

1 Answers1

1

you can use os module to clear terminal output

import os
...

for i in range(9999999999999999999):
    os.system("cls") # if you use windows
    os.system("clear") # if you use Mac or unix like system.
    local_time()
    
George Imerlishvili
  • 1,816
  • 2
  • 12
  • 20