我试图编写一个程序来搜索嵌套目录,然后返回搜索到的目录的位置文件.In为了将扫描嵌套在目录中,我调用了函数中的函数。我是成功的,因为这样做的程序工作,但当我想停止搜索时,第一次击中完成。代码如下。有人能让我明白他为什么不停下来吗。你知道吗
search = sys.argv[2]
def find(loca,term):
count = 1
for file in os.listdir(loca):
if (count > 1):
break
try:
os.chdir(f'{loca}\{file}')
newloca = loca + '\\' + file
find(newloca,term)
except NotADirectoryError:
pass
except PermissionError:
pass
if(file == term):
print(file)
print(f"found the {term} at")
print(os.getcwd())
count += 1
find("E:/",search)
因为您使用的是递归,所以当您发现命中时,您需要某种方式来向父级发出信号。你可以使用
return
。你知道吗相关问题 更多 >
编程相关推荐