Reputation: 17
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
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