![](/img/trans.png)
[英]How do I create a python script such that it sends an email when csv files in a directory has not updated in the last 24 hours?
[英]Trying to create a script that crosschecks if excel file has been updated and if it has been updated, sends out an email
基本上我經營一家商店,我們每天更新一次獲取外部數據的 excel 文件,然后我們通過 email 將更新的文件發送給一群人。 我們通過很多報告來做到這一點,所以我想編寫一個自動執行此操作的腳本。
外部數據每天出現一次,有時在凌晨 1 點出現,有時在凌晨,有時更晚 - 但主要是在夜間/清晨出現,所以當我開始工作時,應該更新外部數據集。
我的問題是:
讓 excel 文件加載並通過 email 發送似乎很簡單,但如果假設外部數據集尚未更新,我不希望發送 email。
我如何將前一天的數據集與今天的數據集進行比較,而不將昨天的數據集作為單獨的文件保存在我的計算機上,因為這會累積很多文件。
基於此鏈接生成文件的 MD5 校驗和
代碼可能如下所示:
import hashlib
from os.path import exists
def get_oldsum():
global last_checksum
if exists("lastsum"):
with open("lastsum","r") as file:
last_checksum = file.readline()
else:
last_checksum = ""
def save_newsum(newsum):
with open("lastsum", "w") as file:
file.write(newsum)
def make_new_hash(file):
with open(file,"rb") as file:
file_hash = hashlib.md5()
while chunk := file.read(8192):
file_hash.update(chunk)
return file_hash.hexdigest()
if __name__ == "__main__":
get_oldsum()
new_hash = make_new_hash("shared_reports.xlsx")
print("This MD5 Hash:",new_hash)
if new_hash != last_checksum:
print("New Hash! We should trigger our mail-function asap...")
save_newsum(new_hash)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.