Discuss / Python / 基于os.walk函数粗略实现了关键字查找文件

基于os.walk函数粗略实现了关键字查找文件

Topic source

Velskerd

#1 Created at ... [Delete] [Delete and Lock User]
import osdef search(path, filename):    dirspool = []    filespool = {}    count = 0    for root, dirs, files in os.walk(path, topdown=True):        for name in dirs:            dir_name = os.path.join(root, name)            dirspool.append(dir_name)        for name in files:            file_name = os.path.join(root, name)            count += 1            filespool[count] = file_name    target = [x[1] for x in filespool.items() if filename in x[1]]    return target

>>> print(search('g:/','ha'))
['g:/cccc\\cccc-b\\haha.txt']



  • 1

Reply