第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

刪除日志文件中超過 10 天的所有行

刪除日志文件中超過 10 天的所有行

當(dāng)年話下 2023-08-15 17:29:30
我有一個(gè)文件夾,我正在嘗試刪除所有超過 10 天的行。輸入如下:error.log20200122024227-vision.log20200122024730-vision 20200122024930 Missing 20200122024730-vision.log20200520210139 Failed :20200811152053-ibm 20200811152254 Missing20200812164636-ibm_global 20200812164837 Missing20200812210311-ibm_global 20200812210512 Missing20200813080856-ivr 20200813081056 Missing20200813092556-chat_global 20200813092757 Missing20200813125528-ibm_global 20200813125728 Missing20200813163610-acaps_global 20200813163810 Missing20200813172428-mvs_global 20200813172629 Missing20200820204216-pos_global 20200820204417 Missing20200910103742-chatbot_global 20200826103943 Missing20200913103742-chatbot_global 20200826103943 Missing20200914103742-chatbot_global 20200826103943 Missing20200915103742-chatbot_global 20200826103943 Missing20200916103742-chatbot_global 20200826103943 Missing我的輸出應(yīng)該是:20200910103742-chatbot_global 20200826103943 Missing20200913103742-chatbot_global 20200826103943 Missing20200914103742-chatbot_global 20200826103943 Missing20200915103742-chatbot_global 20200826103943 Missing20200916103742-chatbot_global 20200826103943 Missing這些日志不到 10 天。首先,我需要從 is 行獲取時(shí)間戳20200916103742,并檢查它是否小于保留期。如果它被保留,我們必須保留,否則刪除。我傳遞給程序的參數(shù)是當(dāng)前時(shí)間戳,保留期為 10 天。所以這意味著我只需要最近 10 天的日志,然后從 error.log 中刪除代碼如下:current_date = sys.argv[1]retention_period  = sys.argv[2]old_date = int(current_date ) - int(redention_period*1000000)path = "abc/log/log_test/"error_file = path+"error.log"file = open(error_file)output = []for line in file:    print(line)    str_log_key = line[0:13]    log_key = int(str_log_key)    if log_key in range(old_date, current_date):        output.append(line)f.close()f = open(error_file, 'w')f.writelines(output)f.close()我收到如下錯(cuò)誤:if log_key in range(old_date, current_date):  TypeError: 'str' object cannot be interpreted as an integer還有其他更好的編碼方式嗎?
查看完整描述

3 回答

?
哆啦的時(shí)光機(jī)

TA貢獻(xiàn)1779條經(jīng)驗(yàn) 獲得超6個(gè)贊

這個(gè)功能可能會(huì)幫助你:


from datetime import date, timedelta


def del_old():


    limit = "".join(str(date.today() - timedelta(days = 10)).split("-"))


    with open("error.log", "r") as file: 

        data = file.readlines()


    with open("error.log", "w") as file2: 


        for line in data: 

            (int(line.lstrip()[:8]) > int(limit)) and file2.write(line)


查看完整回答
反對 回復(fù) 2023-08-15
?
料青山看我應(yīng)如是

TA貢獻(xiàn)1772條經(jīng)驗(yàn) 獲得超8個(gè)贊

假設(shè)present_date您的意思是current_date那么您也需要將其轉(zhuǎn)換為整數(shù)。



查看完整回答
反對 回復(fù) 2023-08-15
?
holdtom

TA貢獻(xiàn)1805條經(jīng)驗(yàn) 獲得超10個(gè)贊

另一種方法是創(chuàng)建一個(gè)新文件:


from datetime import date


errorfile = open(f'error.log', 'r')

errorNew = open(f'error_new.log', 'a+')


current_date = date.today()


for line in errorfile:

    date_line = date(int(line[0:4]), int(line[4:6]), int(line[6:8]))

    diff = (current_date - date_line)

    if diff.days < 10:

        errorNew.write(line)


查看完整回答
反對 回復(fù) 2023-08-15
  • 3 回答
  • 0 關(guān)注
  • 177 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)