seven-zhang
seven-zhang

Reputation: 17

python recursion dir return NONE

1.recursion filesList in dir,if found the file then return the file path

2.print value is true. but always return NONE

def getFilePath(filepath,fileName):
    files = os.listdir(filepath)
    for fi in files:
        fi_d = os.path.join(filepath, fi)
        if os.path.isdir(fi_d):
            getFilePath(fi_d, fileName)
        else :
            if fi_d.find(fileName) == -1:
                continue
            else:
                print fi_d
                return fi_d

Upvotes: 0

Views: 310

Answers (1)

OneCricketeer
OneCricketeer

Reputation: 191728

I think you should return at the end of the function only, otherwise python returns None

Also, need to capture the recursive return

def getFilePath(filepath,fileName):

    for fi in os.listdir(filepath):
        fi_d = os.path.join(filepath, fi)
        if os.path.isdir(fi_d):
            fi_d = getFilePath(fi_d, fileName)
        else :
            if fi_d.find(fileName) == -1:
                continue
            else:
                print fi_d
    return fi_d

Upvotes: 1

Related Questions