我在 python 中的递归函数在 python 中不断返回 none

My recursive function in python keeps returning none in python

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

为什么这个函数 return 是 None 类型而不是我找到的文件的路径?

当我 运行 函数时,递归工作并且能够找到并打印文件的路径,但没有 returned。有人可以解释一下为什么吗?

您没有显式地 return 递归调用的值,因此 隐式地 函数 returns None else支线。相反,使用:

...

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