Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
2 errors in code
#1
I get the following errors from my code:

Error:
attachment = open(Assessments, 'rb') TypeError: expected str, bytes or os.PathLike object, not list
Error:
]File "C:\Users\Matti\Desktop\[COPY]attaching MULTIPLE files.py", line 31, in <module> file_path = os.path.join(dir_path, assess) NameError: name 'dir_path' is not defined
What I'm trying to do is send several attachments (files, not images) and not just one. I managed with one, but for some reason several is hard for me to grasp.
I am learning Python via project building and not tutorials because I can't learn general programming concepts from tutorials using bland short examples and apply it immediately to my project, unfortunately, so I need help here.
I would appreciate any pointers.


 
import os
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders



#Notice we are using Windows "Enviornment Variables", in order to hide our sensitive data
Email_User = os.environ.get("Andree_Email")

Email_Password = os.environ.get("Andree_Pass")

Subject = "Python"

msg = MIMEMultipart()
msg['From'] = Email_User
msg['To'] = "[email protected]"
msg['Subject'] = Subject

body = "Hi there. Sending this email from Python"
msg.attach(MIMEText(body, 'plain'))

#EDITED

Assessments = ["c:\\Users\\Matti\Desktop\\Python video left off.docx", "c:\\Users\\Matti\Desktop\\API variable.png", "c:\\Users\\Matti\Desktop\\mind tricks.docx"]
attachment  = open(Assessments, 'rb')

for assess in Assessments:  # add files to the message
        file_path = os.path.join(dir_path, assess)
        attachment = MIMEApplication(open(file_path, "rb").read(), _subtype="txt")
        attachment.add_header("c:\\Users\\Matti\Desktop\\Python video left off.docx","c:\\Users\\Matti\Desktop\\API variable.png","c:\\Users\\Matti\Desktop\\mind tricks.docx",'attachment', filename=assess)
        msg.attach(attachment)
        


part = MIMEBase('application','octet-stream')
part.set_payload((attachment).read())
encoders.encode_base64(part)
part.add_header('Content-Disposition',"attachment; filename= "+Assessments)


msg.attach(part)
text = msg.as_string()

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()

server.login(Email_User,Email_Password)



server.sendmail(Email_User,"[email protected]",text)

server.quit()
Reply


Messages In This Thread
2 errors in code - by numbnumb54 - Jul-24-2020, 02:05 PM
RE: 2 errors in code - by eismb - Jul-24-2020, 02:21 PM
RE: 2 errors in code - by Marbelous - Jul-24-2020, 02:28 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Are there errors in the code for my coin toss systems? Matlibplot is too perfect . Coolkat 0 405 Nov-13-2023, 11:54 AM
Last Post: Coolkat
  My code won't say Player wins or computer wins. No errors. its_a_meLuigi64 2 1,654 Dec-01-2021, 04:43 PM
Last Post: its_a_meLuigi64
  Rmarkdown opened by python code - errors Rav013 0 2,116 Apr-27-2021, 03:13 PM
Last Post: Rav013
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 3 4,666 Apr-13-2021, 01:26 PM
Last Post: amirian
  Code errors rhyjom 0 1,481 Jun-21-2020, 04:50 PM
Last Post: rhyjom
  Errors in code coolcassie 2 2,153 Dec-03-2019, 12:10 AM
Last Post: coolcassie
  Errors trying to run code ziggyztz 3 3,705 Nov-03-2019, 04:24 AM
Last Post: newbieAuggie2019
  errors in code sylvie1987100 3 2,357 Sep-11-2019, 12:54 PM
Last Post: sylvie1987100
  Decorator toy code throws syntax errors kevinxhi 3 3,607 Sep-04-2017, 03:01 AM
Last Post: kevinxhi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020