user134743
user134743

Reputation: 9

Recursive function with for loop python

I have a question that should not be too hard but it has been bugging me for a long time.
I am trying to write a function that searches in a directory that has different folders for all files that have the extension jpg and which size is bigger than 0.
It then should print the sum of the size of the files that are in these categories.

What I am doing right now is

def myFuntion(myPath, fileSize): 

    for myfile in glob.glob(myPath): 
        if os.path.isdir(myFile):
            myFunction(myFile, fileSize)

        if (fnmatch.fnmatch(myFile, '*.jpg')):
            if (os.path.getsize(myFile) >  1):
                fileSize = fileSize + os.path.getsize(myFile)


    print "totalSize: " + str(fileSize)

This is not giving me the right result. It sums the sizes of the files of one directory but it does not keep summing the rest. For example if I have these paths

C:/trial/trial1/trial11/pic.jpg  

C:/trial/trial1/trial11/pic1.jpg  

C:/trial/trial1/trial11/pic2.jpg  

and

C:/trial/trial2/trial11/pic.jpg  

C:/trial/trial2/trial11/pic1.jpg  

C:/trial/trial2/trial11/pic2.jpg  

I will get the sum of the first three and the the size of the last 3 but I won´t get the size of the 6 together, if that makes sense.

Upvotes: 0

Views: 113

Answers (2)

ronakg
ronakg

Reputation: 4212

You should use os.walk() to solve this problem.

This is how I would do it -

import os
import sys

def get_file_size(path, extensions):
    size = 0

    for root, dirs, files in os.walk(path):
        for file in files:
            if (file.lower().endswith(extensions)):
                size += os.path.getsize(os.path.join(root, file))

    return size

print(get_file_size('.', ('jpg', 'jpeg')))

Upvotes: 1

Martijn Pieters
Martijn Pieters

Reputation: 1123830

You are ignoring the result of the recursive call; fileSize is not shared between calls.

Instead of passing in fileSize to recursive calls, sum the returned sizes:

def myFunction(myPath): 
    fileSize = 0

    for myfile in glob.glob(myPath): 
        if os.path.isdir(myFile):
            fileSize += myFunction(myFile)

        if fnmatch.fnmatch(myFile, '*.jpg'):
            fileSize += os.path.getsize(myFile)


    return fileSize

then print the final total returned by the outermost call:

print myFunction('C:/trial/')

Upvotes: 1

Related Questions