I have a small program that searches through two folders and gets the size of the files in them. As of right now for every file that is over the specified size limit sends a email. What i am wanting to do it have it compile a lsit of the files that are to large and send the list as one email.

import requests, shutil, datetime, glob, os, csv,fnmatch,StringIO,smtplib,argparse, math
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from os import path

parser = argparse.ArgumentParser(description='Search art folders.')
parser.add_argument('-b', help='The base path', required=False, dest='basePath', metavar='Base directory path',default='/home/hatterx/Desktop')
parser.add_argument('--se', help='Change sending emailaddress', required=False, dest='sendEmail', metavar='Sending Emailaddress', default='addon2031@gmail.com')
parser.add_argument('--re', help='Change reciveing emailaddress', required=False,dest='reciveEmail', metavar='Reciveing Emailaddress', default='hatterx@oakleysign.com')
parser.add_argument('--pw', help='Change password', required=False, dest='changePassword', metavar='Change Password', default='jcfutalhndkwsltg')
parser.add_argument('-s', help='Change File size', required=True, dest ='changesize', metavar='Change size', default='0')
args = parser.parse_args()

files = StringIO.StringIO()

def sendEmail(toaddress, subject, body):
    fromaddr=args.sendEmail
    toaddr= toaddress
    msg=MIMEMultipart()
    msg['From']=fromaddr
    msg['To']=toaddr
    msg['Subject']= subject
    body= body
    msg.attach(MIMEText(body,'plain'))
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(Sender, Password)
    text = msg.as_string()
    server.sendmail(fromaddr, toaddr, text)

fF = datetime.datetime.now().strftime("%Y%B")
fA = datetime.datetime.now().strftime("%Y%b")

filebase = args.basePath
fileFull = filebase+'/'+fF.upper()+'JPG'
fileAbriv = filebase+'/'+fA.upper()+'JPG'
Reciver = args.reciveEmail
Sender = args.sendEmail
Password = args.changePassword


def fileSearch(filename):

    os.chdir(filename)
    for fileName in glob.iglob("*"):
        files.write(fileName)
        files.seek(0)
        dirname, filenamE = os.path.split(fileName)
        SUBJECT = 'File size'
        statinfo = os.stat(fileName)
        numToString = str(statinfo.st_size/1024)
        BODY = 'The following list of files are to large: '+files.readline()
        print (str(statinfo.st_size/1024))+'KB'
        print (filename)+'/'+(filenamE)
        if args.changesize >= str(statinfo.st_size/1024):
            sendEmail(args.reciveEmail, SUBJECT, BODY)
        elif args.changesize < str(statinfo.st_size/1024):
            print 
    print 'These files are the correct size please check email for files that are to large.'
fileSearch(fileFull)
fileSearch(fileAbriv)   

Why you are using StringIO here? Just build the string normally and send it.
Python pseudo-code:

send_mail(address, subject,'\n'.join(info_i_want(big_file) for big_file in list_of_files if size_of(big_file) > SIZE_LIMIT)

Edited 2 Years Ago by pyTony

This article has been dead for over six months. Start a new discussion instead.