1

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.

  • `os.listdir` only gives you the files in that path, not the sub-directories. You should take a look at [this](http://stackoverflow.com/questions/973473/getting-a-list-of-all-subdirectories-in-the-current-directory) thread to see how to get the subdirectories – Elias Jun 02 '14 at 22:02
  • 1
    You should use `os.walk`. http://www.tutorialspoint.com/python/os_walk.htm – R Sahu Jun 02 '14 at 22:08
  • Is there a way to do this without os.walk, using recursion? – Banthilicious Jun 02 '14 at 22:35
  • I notice that when you `try`/`except`, you don't do anything with the exception. This could be part of your problem; your code is not notifying you when errors occur. – jpmc26 Jun 03 '14 at 02:29

1 Answers1

0
count = 0

for root, dirs, files in os.walk(path):
    for name in files:
        if  name.upper() == dname.upper():
           count += 1

return count

Update

You are missing a key logic. Don't recurse unless a directory entry corresponds to a sub-directory. Here's a version that works for me.

def countDir(dname, path):
   count = 0
   for item in os.listdir(path):
       if item.upper() == dname.upper():
           count += 1
       else:
           next = os.path.join(path,item)
           if os.path.isdir(next):
              try:
                  count += countDir(dname,next)
              except:
                  return count
   return count
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • this works, but is it recursive? this is for a class, and the homework is supposed to use recursion, I saw this in other posts, and sites but I dont think its what my professor is looking for. – Banthilicious Jun 02 '14 at 22:33
  • @user3701001 If you're doing homework, you're better off coming up with your own solution than using code given to you on StackOverflow. If you don't understand the concepts, this is **going** to hurt you later on, so start by trying to understand their basic principle. A recursive function is just a function that **calls itself**. When you're confused and your professor or book aren't helping, start looking for outside resources. Here's something that I found just by Googling "recursive function": http://www.cs.umd.edu/class/fall2002/cmsc214/Tutorial/recursion2.html. – jpmc26 Jun 03 '14 at 02:26