Python Reference a folder or files in a folder

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 = '[email protected]'
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)

Upvotes: 0

Views: 10375

Answers (1)

Shijo
Shijo

Reputation: 9711

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()

Upvotes: 1

Related Questions