ActPi向阳技术栈
← 返回文章列表

从任意目录查询指定后缀的文件并返回对应完整路径

文章目录
  1. 用于在指定路径查询日志文件并返回对应的完整路径列表
  2. 用 os path
  3. 用 pathlib

用于在指定路径查询日志文件并返回对应的完整路径列表

用 os path

def get_log_file(log_dir):
    """
    从指定目录查找后缀为 .log 的文件
    :param log_dir: 待查找的目录
    :return: 返回所有的 .log 文件路径列表
    """
    log_files = []
    for root, dirs, files in os.walk(log_dir):
        for file in files:
            if file.endswith(".log"):
                log_files.append(os.path.join(root,file))

    return log_files

用 pathlib

def get_log_file(rootdir):
    log_files = []
    root_path = Path(rootdir)
    print("--->开始到算例库查找算例文件: ")
    ### find inp
    for file in root_path.rglob('*.log'):
        log_files.append(file)

    return log_files

改动 if file.endswith(".log") 可查找任意后缀的文件。