Discuss / Python / 在当前目录及子目录下查找文件名包含指定字符串的文件

在当前目录及子目录下查找文件名包含指定字符串的文件

Topic source

苏生不语_

#1 Created at ... [Delete] [Delete and Lock User]
import os
root = os.path.abspath('.')


def find_file(name, path):
    for f in os.listdir(path):
        if os.path.isdir(f):
            find_file(name, os.path.join(path, f))
        elif name in os.path.split(f)[1]:
            print(os.path.join(path, f))


find_file('bak', root)

LevineTran

#2 Created at ... [Delete] [Delete and Lock User]

我觉得你可以把两个if调换下顺序,虽然递归栈都会全部遍历一遍,但是把基准条件先放在前面,可以更快速的出结果


  • 1

Reply