我有这段代码,出于某种原因,当我尝试返回路径时,我得到的是:
def get_path(dictionary, rqfile, prefix=[]):
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))
else:
get_path(directory[filename], rqfile, path)
Run Code Online (Sandbox Code Playgroud)
有办法解决这个问题吗?提前致谢.
Mar*_*ers 19
您需要返回递归结果:
else:
return get_path(directory[filename], rqfile, path)
Run Code Online (Sandbox Code Playgroud)
否则函数只是在执行该语句后结束,导致None
返回.
你可能要下降了else:
,总是返回结尾:
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)
Run Code Online (Sandbox Code Playgroud)
因为如果rqfile in str(os.path.join(*path))
是,False
那么你没有那么好结束你的功能return
.如果在这种情况下,递归是不是正确的选择,但回来None
是不是,你需要处理的是edgecase了.
归档时间: |
|
查看次数: |
13533 次 |
最近记录: |