-1
def recursiveSearch(rootDir):
    file = 'foundMe'

    # Creates a full path back to the root directory with each item in list
    for p in os.listdir(rootDir):
        path = os.path.join(rootDir, p)
        if os.path.isfile(path):
                if os.path.splitext(os.path.basename(path))[0] == file:
                    print("congrats you found", path)

                    return path

        else:
            if os.path.isdir(path):
                recursiveSearch(path)

x = recursiveSearch(rootDir)
print(x) ->>> None

Why does this function return the None type and not the path of the file that I have found?

When I run the function, the recursion works and is able to find and print the path to the file, but nothing is returned. Could someone please explain why?

Enamul Hassan
  • 5,266
  • 23
  • 39
  • 56
Adele
  • 39
  • 2
  • 9

1 Answers1

3

You're not explicitly returning the value of the recursive call, so implicitly the function returns None in the else branch. Instead, use:

...

else:
    if os.path.isdir(path):
        return recursiveSearch(path)
BrianO
  • 1,496
  • 9
  • 12