0

Good Afternoon.

I would like to know how to reference files or a folder to a variable in Python.

Lets say i have some files with the following names:

161225_file1.txt
161225_file2.txt
161225_file3.txt
161226_file1.txt
161226_file2.txt
161226_file3.txt
161227_file1.txt
161227_file2.txt
161227_file3.txt

i want to assign those files to a variable so i can use it in a email script i have, i also only need the files with a certain date.

Right now i only have the following:

#!/usr/bin/env python
###############################
#Imports (some library imports)
###############################
import sys
import time
import os
import re
from datetime import date,timedelta
from lib.osp import Connection
from lib.sendmail import *


###############################
#Parameters (some parameters i`ll use to send the email, like the email body message with yesterdays date)
###############################

ndays = 1
yesterday = date.today() - timedelta(days=ndays)
address = 'some.email@gmail.com'
title = '"Email Test %s"' % yesterday
attachments = ''

mail_body = "This is a random message with yesterday`s date %s" % yesterday


###############################
#Paths (Path that contain the home directory and the directory of the files i want to attach)
###############################


homedir = '/FILES/user/emails/'
filesdir = '/FILES/user/emails/TestFolder/'


###############################
#Script
###############################

#starts the mail function  

start_mail()


#whatever message enters here will be in the email`s body.
write_mail(mail_body)



#gets everything you did before this step and sends it vial email.
send_mail(address,title,attachments)
  • Possible duplicate of [How to send email attachments with Python](http://stackoverflow.com/questions/3362600/how-to-send-email-attachments-with-python) – Carles Mitjans Dec 29 '16 at 15:26
  • What is the issue/question? – mikerose Dec 29 '16 at 15:58
  • Are you trying to load the filenames into a list? Are you trying to read the contents of the files? Please be more specific. – 2rs2ts Dec 29 '16 at 16:00
  • If someone nefarious were to see this, they might use something like this to create spam. Spam is not a good thing. It uses more energy globally than cryptocurrency. Its carbon footprint is something like 1/3 of all internet traffic. – EngrStudent Feb 02 '22 at 15:47

1 Answers1

1

os.listdir() will get files and subdirectories in a directory.

If you want just files,use os.path:

from os import listdir
from os.path import isfile, join
    files = [f for f in listdir(homedir) if isfile(join(homedir, f))]

And you can find detailed explantion on how to attach file to an email here Thanks to Oli

import smtplib
from os.path import basename
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.utils import COMMASPACE, formatdate


def send_mail(send_from, send_to, subject, text, files=None,
              server="127.0.0.1"):
    assert isinstance(send_to, list)

    msg = MIMEMultipart()
    msg['From'] = send_from
    msg['To'] = COMMASPACE.join(send_to)
    msg['Date'] = formatdate(localtime=True)
    msg['Subject'] = subject

    msg.attach(MIMEText(text))

    for f in files or []:
        with open(f, "rb") as fil:
            part = MIMEApplication(
                fil.read(),
                Name=basename(f)
            )
            part['Content-Disposition'] = 'attachment; filename="%s"' % basename(f)
            msg.attach(part)


    smtp = smtplib.SMTP(server)
    smtp.sendmail(send_from, send_to, msg.as_string())
    smtp.close()
Community
  • 1
  • 1
Shijo
  • 9,313
  • 3
  • 19
  • 31