Recursive function returning none in Python [duplicate]

You need to return the recursive result:

else:
   return get_path(directory[filename], rqfile, path)

otherwise the function simply ends after executing that statement, resulting in None being returned.

You probably want to drop the else: and always return at the end:

for filename in dictionary.keys():
    path = prefix+[filename]
    if not isinstance(dictionary[filename], dict):

        if rqfile in str(os.path.join(*path)):
            return str(os.path.join(*path))

    return get_path(directory[filename], rqfile, path)

because if rqfile in str(os.path.join(*path)) is False then you end your function without a return as well. If recursing in that case is not the right option, but returning None is not, you need to handle that edgecase too.

Leave a Comment