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

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

如何在 Python 中將日期時間保存到文件以用于腳本自動化

如何在 Python 中將日期時間保存到文件以用于腳本自動化

撒科打諢 2023-05-09 10:32:49
我正在創(chuàng)建一個腳本,該腳本會定期為服務(wù)器抓取已添加的“新”文件。為此,我希望將上次腳本執(zhí)行的日期時間存儲在一個文件中,以便腳本可以處理自該日期時間以來的“所有新文件”。最終目標(biāo)是通過 Windows 任務(wù)計劃程序定期運(yùn)行此腳本。我可以使用下面的代碼來做這個的基本版本。但是,我希望有一種更簡潔、更短或更健壯的方法來實現(xiàn)這一目標(biāo)。歡迎任何建議!import datetimefmt = "%Y-%m-%d %H:%M:%S"last_run = ""# try loading the datetime of the last run, else print warningtry:    with open("last_run.txt", mode="r") as file:        last_run = datetime.datetime.strptime(file.read(), fmt)        print(last_run)except:    print("no file available")# ... run script code using the last_run variable as input ...# update the script execution time and save it to the filewith open("last_run.txt", mode="w") as file:    file.write(datetime.datetime.now().strftime(fmt))
查看完整描述

1 回答

?
呼啦一陣風(fēng)

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

您的解決方案看起來不錯。


我唯一想建議的是在兩個單獨的函數(shù)中取出讀取和寫入上次運(yùn)行時間戳邏輯,并將這兩個函數(shù)移動到一個單獨的模塊文件中。這與上述回復(fù)中@Tomalak 的建議相同。下面是代碼示例。


模塊文件:last_run.py


import datetime


fmt = "%Y-%m-%d %H:%M:%S"



def get_last_run_time_stamp():

    """

    Get last run time stamp\n

    ====\n

    When this function called\n

    AND  last_run.txt file is present\n

    Then open the file and read the time-stamp stored in it\n

    ====\n

    When this function is called\n

    AND last_run.txt file is not present\n

    Then print the following message on console: "last_run.txt file is not available"\n

    """

    # try loading the datetime of the last run, else print warning

    try:

        with open("last_run.txt", mode="r") as file:

            return datetime.datetime.strptime(file.read(), fmt)

    except:

        # Return with current time-stamp if last_run.txt file is not present

        return datetime.datetime.now().strftime(fmt)



# ... run script code using the last_run variable as input ...


def save_last_run_time_stamp():

    """

    Save last run time stamp\n

    ====\n

    When this function called\n

    AND  last_run.txt file is present\n

    Then Open the file, save it with current time stamp and close the file\n

    ====\n

    When this function called\n

    AND  last_run.txt file is not present\n

    Then Create the file, open the file, save it with current time stamp and close the file\n

    """

    # update the script execution time and save it to the file

    with open("last_run.txt", mode="w") as file:

        current_timestamp = datetime.datetime.now().strftime(fmt);

        file.write(current_timestamp)

然后,下面是 schedular 配置和運(yùn)行的文件:


run_latest_scaped_files.py,


import last_run as lr


last_run = lr.get_last_run_time_stamp()

print(last_run)


# ... run script code using the last_run variable as input ...


lr.save_last_run_time_stamp()

就是這樣?。?!


查看完整回答
反對 回復(fù) 2023-05-09
  • 1 回答
  • 0 關(guān)注
  • 219 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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