import os
import sys
sys.setrecursionlimit(1000000)
def searchFilePath(filename, path):
try:
for direction in os.listdir(path):
if direction == filename:
print(path)
break
elif os.path.isfile(os.path.join(path, direction)):
continue
else:
searchFilePath(filename, os.path.join(path, direction))
except PermissionError:
pass
searchFilePath("abc.rar", "d:\\")
每次程序找到目标文件时,它都不能立即停止,尽管我使用break结束递归。它总是遍历路径下的所有路径,然后返回结果
你用错了
break
break
将您从单个循环中分离出来这将继续循环
another_list
中的所有值您想使用
return
,这将使您脱离整个函数请注意,这将在通过
yet_another_list
的第一次迭代中停止,并且仅作为说明代码不起作用的示例break
仅结束电流回路。调用堆栈中任何正在进行的循环都不会退出您必须从函数返回一个标志,这样父调用也可以知道退出:
break
只留下当前循环。必须使用返回值来表示递归的结束或者如果您想进一步使用
path
:如果使用
os.walk
,可以将函数简化为:相关问题 更多 >
编程相关推荐