Discuss / Python / 第二题答案

第二题答案

Topic source

绊倒铁盒

#1 Created at ... [Delete] [Delete and Lock User]
import os
def find_file(file_name):

    if not isinstance(file_name, str):
        raise ValueError('请输入正确的待检索的文件名称')
    target_file_path = list()
    for file in os.listdir('.'):
        match(file_name, file, target_file_path)
    return target_file_path


def match(file_name, file, file_path_list):
    if os.path.isfile(file):
        if os.path.split(file)[1].find(file_name) != -1:
            file_path_list.append(os.path.split(file)[1])
    elif os.path.isdir(file):
        for local_file in os.listdir(os.path.abspath(file)):
            match(file_name, local_file, file_path_list)


  • 1

Reply