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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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))
if not log_files:
print("未发现任何 log 文件!")
sys.exit(2)

return log_files

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