简体   繁体   中英

Is there is a way to delete the last line in a terminal?

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?

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()
    

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM