Trying to recursively traverse folders using python,
for item in os.listdir(path):
print('in ' + os.path.join(path,item))
if item.upper() == dname.upper():
print('FOUND')
next = os.path.join(path,item)
return 1 + countDir(dname,next)
else:
try:
next = os.path.join(path,item)
countDir(dname,next)
except:
return 0
it stops at the first item in for loop and Ive wracked my brain trying to figure out why.
I get this
countDir('dir1','testThree')
in testThree\dir1
FOUND
in testThree\dir1\Dir1
FOUND
in testThree\dir1\Dir1\dirA
in testThree\dir1\Dir1\dirA\file.txt
in testThree\dir1\Dir1\prog.py
2
but there are 2 more directories it should be going into after dir1. that contain 2 more Dir1's so I should be returning 4 instead of the 2.