I am attempting to create a python script that checks to see if a file was created today in a couple linux directories, and if they were created, send an email to a distribution group with the name of the files created. If it was not, send an email stating that the files were not created today. I am having trouble with my current code to be able to extract the actual file name/date and send it in my email report.
This is a linux server running Python 2.7.5
import datetime as dt
import os
import smtplib
today = dt.datetime.now().date()
for file in os.listdir('/gma/cache/completed'):
filetime = dt.datetime.fromtimestamp(
os.path.getctime('/gma/cache/completed' + file))
print(filetime)
if filetime.date() == today:
SERVER = 'smtp.gmail.com'
FROM = 'llara@abc.com'
TO = ['generic@gmail.com']
SUBJECT = 'gma reporting.. '
TEXT = ''
message = """From: %s\r\nTo: %s\r\nSubject: %s\r\n\
%s
""" % (FROM, ", ".join(TO), SUBJECT, TEXT)
server = smtplib.SMTP(SERVER)
server.login('user', "PASSWORD")
server.sendmail(FROM, TO, message)
server.quit()
I am currently awaiting approval to test this in a sandbox environment, and I wanted some feedback.