I'm kind of a beginner in programming. With Python (3.5) I'm trying to create a script that scrapes a table from the web and writes a csv that is to be sent by email to a recipient. Thanks to various questions here at stvrflw I've managed to go nearly all the way. Problem, though, my csv-attachment is empty?!? What am I missing?? Thanks!
....
#code above is to scrape info and create csv specified below. Works just fine!
header="Date;Index;Country;%;+/-;Latest;High;Low;%1mon;%3mon;%6mon;%year;%1yr;Time"
file=open(os.path.expanduser("Windex"+dte+".csv"),"wb")
file.write(bytes(header, encoding="ascii",errors='ignore'))
file.write(bytes(kdata, encoding="ascii",errors='ignore'))
SUBJECT="Windex"+dte
msg = MIMEMultipart()
msg['From'] = 'xxx@xxx.com'
msg['To'] = 'xxx@xxx.com'
msg['Date'] = formatdate(localtime=True)
msg['Subject'] = SUBJECT
files= ['Windex'+dte+'.csv']
for f in files:
part = MIMEBase('application', "octet-stream")
part.set_payload(open(f,"rb").read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', 'attachment; filename="{0}"'.format(os.path.basename(f)))
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com',587)
server.ehlo()
server.starttls()
server.login('xxx','xxx')
server.sendmail('xxx', 'xxx', msg.as_string())