感兴趣Python 查找某个目录下包含某个关键字的所有
文件夹及子
文件夹的
文件名的小伙伴,下面一起跟随编程之家 jb51.cc的小编来看看吧。
# 编程之家 (jb51.cc)
#coding = utf-8
import os
#from functools import reduce
def search(path,s):
#result = [filename for t in os.walk(path) for filename in t[2] if s in os.path.splitext(filename)[0]]
result = []
for t in os.walk(path): #返回的是root,dirs,files
for filename in t[2]: #t[2]指的就是files
if s in os.path.splitext(filename)[0]: #test.txt [0]为test [1]为.txt 文件名和扩展名
result.append(filename)
return result
if __name__ == '__main__':
result = search('D:\py','learning')
print(result)
# End 512.笔记 jb51.cc
原文链接:https://www.f2er.com/python/526834.html