I am new to python. I had a requirement to check the file modified date and compare with current date and send mail if the modified date is older than current date. Below is the script.
import os.path
from datetime import datetime
import smtplib
from smtplib import SMTPException
import logging
logging.basicConfig(
filename="pySysLog.log",
level=logging.INFO,
format="%(asctime)s - %(name)s- %(levelname)s - %(message)s"
)
def sendmail():
sender = 'sntspo@in.inm.com'
receivers = ['Guru.kar@inm.com']
message = """From: SMT Support <sntspo@in.inm.com>
To: Guruprasad <Guru.kar@inm.com>
MIME-Version: 1.0
Content-type: text/html
Subject: PIR File Not Recived from GDMIS
Hi All,</br>
</br>
Today, PIR Feed File is not received from GDMIS</br>
</br>
Regards,</br>
SMT Team
"""
try:
smtpObj = smtplib.SMTP('ni.gelay.inm.com')
smtpObj.sendmail(sender, receivers, message)
smtpObj.quit()
logging.info("Successfully sent email")
except SMTPException:
logging.info("Error: unable to send email")
def checkFile():
uplDate = str(datetime.fromtimestamp(os.path.getmtime("/home/sadm/batchscript/Report.txt")))
currentDate = str(datetime.now())
logging.info("Modified date of file " + uplDate)
logging.info("Current date " + currentDate)
if uplDate < currentDate:
logging.info("File is not received from MIS")
sendmail()
else:
logging.info("File is received from MIS")
checkFile()
I am facing the following issues:
please help
uplDate = datetime.fromtimestamp(os.path.getmtime(file_path))
# uplDate = datetime.fromtimestamp(os.path.getmtime("HCLNotesInstall.log"))
currentDate = datetime.now()
logging.info("Modified date of file " + str(uplDate.date()))
logging.info("Current date " + str(currentDate.date()))
if uplDate.date() < currentDate.date():
uplDate.date() < currentDate.date()
uses a date to compare instead of a string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.