0

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.

javaGuy123456
  • 41
  • 1
  • 1
  • 9

1 Answers1

1

Check out this post. Using os.path.getmtime will give you modified time, but using os.path.getctime doesn't seem to work on Linux.

It looks like you might have a bug here:

filetime = dt.datetime.fromtimestamp(
            os.path.getctime('/gma/cache/completed' + file))

Your os.path.getctime(... piece is adding file to the path without a file separator. Consider using os.path.join, rather than simple string concatenation.

Some small feedback: I would consider factoring your constant definitions outside of your function. E.g., declare SERVER = 'smtp.gmail.com' right below your imports in your module.

Consider using with for your smtp calls. So it would be something like this:

with smtplib.SMTP(SERVER) as s:
    server.login('user', "PASSWORD")
    ...

I would also consider using Python's email module, though for something so simple that may be overkill.

Some high-level feedback on a more "Pythonic" organization:

I would put your code in a function called main (or whatever you find most useful), and then create an if __name__ == '__main__': block at the bottom, which then calls your main function. As your script/program inevitably grows, this will allow other modules to import from this one without running all the code.

Finally, I would strongly encourage the use of a virtual environment on a server. Presumably this a RedHat server (since it's running Python 2.7.5). I've gotten myself in bad situations using the system's Python installation - you can quickly find yourself with a broken system as soon as you need to install a package. Here's a handy guide on virtual environments in Python.

blthayer
  • 782
  • 1
  • 6
  • 19
  • @javaGuy123456 - if this answer effectively answers your question, please consider accepting it. Thanks! – blthayer Jul 04 '19 at 14:55