So i have a python code that searches for a folder in a directory and gets the size of the folder. I am wanting to search inside the folder and get the file size of what is in the folder. The items in the folder will have different extentions so if there is any way to do this it would be a great help.

import requests, shutil, datetime, glob, os, csv,fnmatch,StringIO,smtplib,argparse
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', defauault='')
parser.add_argument('--re', help='Change reciveing emailaddress', required=False,dest='reciveEmail', metavar='Reciveing Emailaddress', default='')
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')
parser.add_argument('-S', help='Change File Size', required=True, dest ='changeSize', metavar='Change Size', default='0')
args = parser.parse_args()

fF ="%Y%B")
fA ="%Y%b")

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

size = path.getsize(fileFull)

Size = path.getsize(fileAbriv)

SUBJECT = 'File size of '+fileFull
Subject = 'File size of '+fileAbriv
BODY = 'The file size of '+fileFull+' was greater than '+str(size)+'.'
Body = 'The file size of '+fileAbriv+' was greater than '+str(Size)+'.'

if args.changesize >= str(size):
    sendEmail(args.reciveEmail, SUBJECT, BODY)
    print (size)
    print (fileFull)

if args.changeSize >= str(Size):
    sendEmail(args.reciveEmail, Subject, Body)
    print (Size)
    print (fileAbriv)

You will have to use a recursive algorithm to compute the size of a folder including all the files and sub-directories (and again all the files in that sub-directory). Use suggestions from this thread for that recursive function. Also, don't use string concatenation for path creation, use os.path.join for that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.