def func(path,list,x):
list2 = os.listdir(path)
for lis in list2:
path2 = os.path.join(path,lis)
if os.path.isfile(path2) and x in lis:
list.append(path2)
elif os.path.isdir(path2):
func(path2,list,x)
def main(x): # 封装,接收str返回包含str的列表
path = os.getcwd()
list = []
func(path,list,x)
return list
**PS:**1.要注意循环中使用return会打破循环,所以应该直接调用递归函数 func(x)而不是return func(x) 2. os.path.abspath只是相对的绝对路径不是真正的绝对路径,使用os.path.join更保险。
Sign in to make a reply
人玉匆花
def func(path,list,x):
list2 = os.listdir(path)
for lis in list2:
path2 = os.path.join(path,lis)
if os.path.isfile(path2) and x in lis:
list.append(path2)
elif os.path.isdir(path2):
func(path2,list,x)
def main(x): # 封装,接收str返回包含str的列表
path = os.getcwd()
list = []
func(path,list,x)
return list
**PS:**1.要注意循环中使用return会打破循环,所以应该直接调用递归函数 func(x)而不是return func(x) 2. os.path.abspath只是相对的绝对路径不是真正的绝对路径,使用os.path.join更保险。