I have currently set up a process for R files where it detects if .rout file contains certain string. However, Python files do not output .rout files. I have a process that writes a csv on a daily basis to an os directory. The logic I want to use is that if that file doesn't update on a certain day, this means, the script failed and I want to get an email alert. So lets say a file in a path
path = 'C:/Python'
file = Data.csv
I want to receive an email whenever the file timestamp is not updated every 24 hours using my below code logic.
My current code for Rout files-
import pandas as pd
import smtplib
from email.message import EmailMessage
import glob
import os
import shutil
df = pd.read_fwf(r'Service-Now-Data.Rout', header=None)
end_str = '- END -'
cols_to_check = ["0"]
def email_alert(subject,body,to):
msg = EmailMessage()
msg.set_content(body)
msg['subject'] = subject
msg['to'] = to
user = "DataScienceScriptAlerts@chxx.com"
msg['from'] = user
server = smtplib.SMTP("smtprelay.corp.chxx.com", 25)
server.starttls()
#server.login(user,password)
server.send_message(msg)
server.quit()
src = r'C:/R'
dest = r'C:/R/Failed Scripts'
if __name__ == '__main__':
for col in cols_to_check:
if not df[0].str.contains(end_str).any():
body = "The Service-Now-Data.R script in PMIV312 had errors on the last execution" + col + "."
print(body)
email_alert("Service-Now-Data failure alert",body,"htvldba@chxx.com")
if not df[0].str.contains(end_str).any():
for file_path in glob.glob(os.path.join(src,'*.Rout'), recursive=True):
new_path = os.path.join(dest, os.path.basename(file_path))
shutil.copy(file_path, new_path)