Recursive function with for loop python
Posted
by
user134743
on Programmers
See other posts from Programmers
or by user134743
Published on 2014-06-05T20:24:17Z
Indexed on
2014/06/05
21:40 UTC
Read the original article
Hit count: 196
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 suming 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.
Thank you so much for your help!
© Programmers or respective owner