0

I'm very new to python.

I have a folder called "etc" with a filename password.txt when generates every night. I would like to run a python script in windows task on daily basis to check if password.txt doesn't exist then send email to [email protected] else do not send any email. I want to trigger email based on the below condition. When the condition is "false" send email else no action taken. How can I achieve it, any help on this would be greatly appreciated.

os.path.isfile("/etc/password.txt") True

Kind regards,

Biswa

2
  • post the code you have tried Commented Apr 21, 2020 at 8:39
  • 2
    you can use the task schedualer to scheduale your script to run daily. you can use smtplib to send eails, here is an example Commented Apr 21, 2020 at 8:43

1 Answer 1

1

Check if File Exists using the os.path Module

The os.path module provides some useful functions for working with pathnames. The module is available for both Python 2 and 3

import os.path

if os.path.isfile('filename.txt'):
    print ("File exist")
else:
    print ("File not exist")

Then to send an email you can use smtplib (one topic here)

import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText

msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'simple email in python'
message = 'here is the email'
msg.attach(MIMEText(message))

mailserver = smtplib.SMTP('smtp.gmail.com',587)
# identify ourselves to smtp gmail client
mailserver.ehlo()
# secure our email with tls encryption
mailserver.starttls()
# re-identify ourselves as an encrypted connection
mailserver.ehlo()
mailserver.login('[email protected]', 'mypassword')

mailserver.sendmail('[email protected]','[email protected]',msg.as_string())

mailserver.quit()
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer. Is there any way i can specify file format like *.txt instead of specifying the name of the file?
Then glob is what you need import glob if glob.glob('*.csv'): print ("File exist")

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.