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
#2
Regarding the TypeError: you are trying to open a list with three strings, instead of opening each string at a time.

Trying putting the open() statement inside the loop, and then using:

for assess in Assessments:  # add files to the message
    attachment  = open(assess, 'rb')
As for the NameError, you are using a variable, "dir_path", that wasn't previously defined within your code, so it doesn't exist yet. Define it somewhere at the beginning with the proper base path.
Reply
#3
The open() function needs a path to a file but you have given it a list of paths. You then go on to refine attachment anyway so I'm not sure what you are trying to do there.

Your second error is simply because you have not defined the variable dir_path. Did you mean to use os.path.dirname() or something???
"So, brave knights, if you do doubt your courage or your strength, come no further, for death awaits you all with nasty, big, pointy teeth!" - Tim the Enchanter
Reply


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 369 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,621 Dec-01-2021, 04:43 PM
Last Post: its_a_meLuigi64
  Rmarkdown opened by python code - errors Rav013 0 2,085 Apr-27-2021, 03:13 PM
Last Post: Rav013
  [split] Kera Getting errors when following code example Image classification from scratch hobbyist 3 4,607 Apr-13-2021, 01:26 PM
Last Post: amirian
  Code errors rhyjom 0 1,458 Jun-21-2020, 04:50 PM
Last Post: rhyjom
  Errors in code coolcassie 2 2,111 Dec-03-2019, 12:10 AM
Last Post: coolcassie
  Errors trying to run code ziggyztz 3 3,653 Nov-03-2019, 04:24 AM
Last Post: newbieAuggie2019
  errors in code sylvie1987100 3 2,300 Sep-11-2019, 12:54 PM
Last Post: sylvie1987100
  Decorator toy code throws syntax errors kevinxhi 3 3,556 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