Discuss / Python / 第二题

第二题

Topic source

亦夜

#1 Created at ... [Delete] [Delete and Lock User]
def find(s: str, cur='.'):
    L = []
    if s.__len__() == 0:
        return L
    s = s.strip()

    def dfs(cur):
        for n in os.listdir(cur):
            # 如果不拼接下一目录则无法判断
            next_dir = os.path.join(cur, n)
            # 目录则递归
            if os.path.isdir(next_dir):
                dfs(next_dir)
            # 文件则返回绝对路径
            elif os.path.isfile(next_dir) and s in os.path.splitext(n)[0]:
                L.append(os.path.join(os.path.abspath('.'), n))
    return dfs(cur) or L

  • 1

Reply