New to Python. Im able to create script fail alerts for R files because the cmd files generate .Rout files(in this case LocationToBlockGroup.Rout) My python script-
import pandas as pd
import smtplib
from email.message import EmailMessage
import glob
import os
import shutil
df = pd.read_fwf(r'LocationToBlockGroup.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@xx.com"
msg['from'] = user
server = smtplib.SMTP("smtprelay.corp.xx.com", 25)
server.starttls()
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 LocationToBlockGroup.R script in xx had errors on the last execution " + col + "."
print(body)
email_alert("LocationToBlockGrp failure alert", body, "htvldba@xx.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)
But how do I do this for python scripts that do not generate .Rout files?is there a python equivalent or something I need to add in the .bat script itself? I just want that if the batch file fails for a scheduled python script, it should send an email alert. This is achieved as I stated above for R files but not sure how to replicate for python files.Am i over engineering this and i can just add a simple line in my .bat file that can do the same?