简体   繁体   English

python递归目录返回NONE

[英]python recursion dir return NONE

1.recursion filesList in dir,if found the file then return the file path 1.递归文件在目录中列出,如果找到文件则返回文件路径

2.print value is true. 2.print值为true。 but always return NONE 但总是返回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

I think you should return at the end of the function only, otherwise python returns None 我认为您应该只在函数结尾处返回,否则python返回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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM