tools.py
1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json
import os
def get_exclude_paths(input_path, exclude_list=[]):
"""
Args:
input_path: str 目标目录
exclude_list: list 排除文件或目录的相对位置
Returns: set 排除文件或目录的绝对路径集合
"""
exclude_paths_set = set()
if os.path.isdir(input_path):
for path in exclude_list:
abs_path = path if os.path.isabs(path) else os.path.join(input_path, path)
if not os.path.exists(abs_path):
print('Warning: exclude path not exists: {0}'.format(abs_path))
continue
exclude_paths_set.add(abs_path)
return exclude_paths_set
def get_file_paths(input_path, suffix_list, exclude_list=[]):
"""
Args:
input_path: str 目标目录
suffix_list: list 搜索的文件的后缀列表
exclude_list: list 排除文件或目录的相对位置
Returns: list 搜索到的相关文件绝对路径列表
"""
exclude_paths_set = get_exclude_paths(input_path, exclude_list)
for parent, _, filenames in os.walk(input_path):
if parent in exclude_paths_set:
print('Info: exclude path: {0}'.format(parent))
continue
for filename in filenames:
for suffix in suffix_list:
if filename.endswith(suffix):
file_path = os.path.join(parent, filename)
break
else:
continue
if file_path in exclude_paths_set:
print('Info: exclude path: {0}'.format(file_path))
continue
yield file_path
def load_json(json_path):
"""
Args:
json_path: str JSON文件路径
Returns: obj JSON对象
"""
with open(json_path, 'r') as fp:
output = json.load(fp)
return output