请问为什么在函数内部打印Path就是找到的文件的绝对路径,用实参接受返回值打印出来就只有None了
import os
def searchFile(Path, File):
allFile = os.listdir(Path)
for eachFile in allFile:
if os.path.isdir(Path + os.sep + eachFile):
searchFile(Path + os.sep + eachFile, File)
elif eachFile == File:
return str(Path)
path = input('请输入待查找的初始目录:')
file = input('请输入需要查找的目标文件:')
print(searchFile(path, file))
def searchFile(Path, File):
allFile = os.listdir(Path)
for eachFile in allFile:
if os.path.isdir(Path + os.sep + eachFile):
return searchFile(Path + os.sep + eachFile, File)
elif eachFile == File:
return str(Path)
如果是文件夹的话,进入下一层递归,但是找到文件后需要返回才可以,如果找不到,结果就是None,就继续查找下一个目录。
def searchFile(Path, File):
allFile = os.listdir(Path)
for eachFile in allFile:
if os.path.isdir(Path + os.sep + eachFile):
res = searchFile(Path + os.sep + eachFile, File)
if res: return res
elif eachFile == File:
return str(Path)