Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- cnopts = pysftp.CnOpts()
- cnopts.hostkeys = None
- import pysftp
- import sys
- import os
- import smtplib
- from datetime import datetime
- from email.mime.text import MIMEText
- def main():
- dir = []
- files = []
- try:
- srv = pysftp.Connection(host="xxxx", username="xxxx", password="xxxx" )
- except Exception as e:
- send_email(u'Connection error: ' + e.message)
- with srv.cd():
- srv.chdir("file location")
- files = srv.listdir()
- dt = datetime.now().strftime('%Y%m%d')
- files = [f for f in files if 'SOPO_147_Consolidated_Plant_BATCHECP_{}_0'.format(dt) in f]
- if len(files) != 1:
- send_email('Error: No file found on FTP path. Please check whether file is posted for today')
- return
- _filename = "filelocation" + files[0]
- srv.get(_filename)
- # Closes the connection
- srv.close()
- # rename the file
- new_filename = 'location'
- # new_filename = 'Location'
- try:
- os.remove(new_filename)
- except Exception as e:
- send_email(u'Move error: ' + e.message)
- os.rename(files[0], new_filename)
- def send_email(message):
- ''' Send email to support alias'''
- msg = MIMEText(message)
- sender = 'shaan.tableau@gmail.com'
- support_alias = 'shaan.tableau@gmail.com'
- msg['Subject'] = 'Test file '
- msg['From'] = sender
- msg['To'] = support_alias
- # Send the message via our own SMTP server, but don't include the
- # envelope header.
- s = smtplib.SMTP('smtp.net')
- s.sendmail(sender, [support_alias], msg.as_string())
- s.quit()
- if __name__ == '__main__':
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement