Oct-03-2018, 07:27 PM
You should just be able to replace
attachment = open(file_location, "rb") part = MIMEBase('application', 'octet-stream') part.set_payload((attachment).read())with
part = MIMEBase('application', 'octet-stream') with open(file_location, "rb") as attachment: part.set_payload(attachment.read())You could also explicit call
attachment.close()
after you read from the file, but context managers (the with-block here) are usually preferred.