首页 > 解决方案 > 遍历目录并获取信息表单文件名

问题描述

我有以下文件结构:

 C:\file1:
   ->test1
     -> 2021_08_18_16_45_09-jbkjjkbkuiuhi
       -> LOG_2021_08_18_16_45_09.txt
     -> 2021_09_09_14_35_39-hgkhbkbhkb
       -> LOG_2021_09_09_14_35_39.txt
     -> 2021_05_07_06_15_19-jkbkjbkjb
       -> LOG_2021_05_07_06_15_19.txt
   ->test2
     -> 2021_07_18_16_45_09-hbkbnnansa
       -> LOG_2021_07_18_16_45_09.txt
   ->test3
     -> 2021_06_19_11_25_29-hbjkhbj
       -> LOG_2021_06_19_11_25_29.txt
     -> 2021_03_08_12_41_01-jhbjhbbjkjbkj
       -> LOG_2021_03_08_12_41_01.txt

每个 LOG 文件的结构如下:

...
...
2021_08_18_16_45_09    PASSED

wrere instaed of pass 也可以FAILED/ PASSED WITH WARNINGS等等

我需要获取名称列表:

tests = ['test1', 'test2', 'test3']

和时间戳:

timestamps = [2021_08_18_16_45_09, 2021_09_09_14_35_39, 2021_05_07_06_15_19, 2021_07_18_16_45_09, 2021_06_19_11_25_29, 2021_03_08_12_41_01]

以及 LOG 文件的所有路径:

all_paths = [C:\file1\test1\2021_08_18_16_45_09-jbkjjkbkuiuhi\LOG_2021_08_18_16_45_09.txt, ...]

以及每个测试的结果(我的日志文件中存在的字符串之一PASSED/FAILED/ PASSED WITH WARNINGS:)

问题是如何以最佳方式有效地获取此类数据?

标签: pythonfile

解决方案


推荐阅读