pythonfilerandomdirectorymime-mail

Select a random file from a directory and send it (Python, MIME)


I am working on a python program that randomly selects a file from a directory and then sends it to you using the email.mimemodule. I am having a problem where I can choose the random file but I can't sent it due to this error:

 File "C:\Users\Mihkel\Desktop\dnak.py", line 37, in sendmemeone
    attachment  =open(filename, 'rb')
TypeError: expected str, bytes or os.PathLike object, not list

Here is the code:

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

path ='C:/Users/Mihkel/Desktop/memes'
files = os.listdir(path)
index = random.randrange(0, len(files))
print(files[index])

def send():
    email_user = 'yeetbotmemes@gmail.com'
    email_send = 'miku.rebane@gmail.com'
    subject = 'Test'
    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To']   = email_send
    msg['Subject'] = subject
    body = 'Here is your very own dank meme of the day:'
    msg.attach(MIMEText (body, 'plain'))
    filename=files
    attachment  =open(filename, 'rb')
    part = MIMEBase('application','octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition',"attachment; 
    filename= "+filename)
    msg.attach(part)
    text = msg.as_string()
    server = smtplib.SMTP('smtp.gmail.com',587)
    server.starttls()
    server.login(email_user,"MY PASSWORD")
    server.sendmail(email_user,email_send,text)
    server.quit()

I believe it is just getting the filename as the selected random choice, how could I get it to select the file itself?

EDIT: After making the changes recommended I am now getting this error:

File "C:\Users\Mihkel\Desktop\e8re.py", line 29, in send
    part.add_header('Content-Disposition',"attachment; filename= "+filename)
TypeError: can only concatenate str (not "list") to str

Seems like this part is still taking in the list, how would I fix that?


Solution

  • You select a random file and then throw it away (well, you print it, then throw it away):

    files = os.listdir(path)
    index = random.randrange(0, len(files))
    print(files[index])
    

    (which BTW you can do with random.choice(files))

    and when calling open you pass it the entire files list:

    filename = files
    attachment  = open(filename, 'rb')
    

    Instead, pass open the file you selected:

    attachment  = open(random.choice(files), 'rb')
    

    But, this still wouldn't work since listdir only returns the filenames and not the full path, so you will need to get it back, preferably with os.path.join:

    attachment  = open(os.path.join(path, random.choice(files)), 'rb')