用于在指定路径查询日志文件并返回对应的完整路径列表
用 os path
1 2 3 4 5 6 7 8 9 10 11 12 13
| 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
1 2 3 4 5 6 7 8 9
| def get_log_file(rootdir): log_files = [] root_path = Path(rootdir) print("--->开始到算例库查找算例文件: ") for file in root_path.rglob('*.log'): log_files.append(file)
return log_files
|
改动 if file.endswith(".log") 可查找任意后缀的文件。