Safemotion Lib
Loading...
Searching...
No Matches
utils_os.py
Go to the documentation of this file.
1import os
2import json
3import numpy as np
4import shutil
5
6def search_file(folder, fileEx):
7 """
8 폴더에서 특정 확장자의 파일을 검색하는 기능
9 하위 폴더까지 모두 검색
10 args:
11 folder (str): 검색하려는 폴더
12 fileEx (str): 파일 확장자
13 return:
14 file_name_list (list[str]): 파일 이름 리스트
15 file_path_list (list[str]): 파일 경로 리스트
16 """
17
18 file_path_list = []
19 for path, dirs, files in os.walk(folder):
20 if path.split('/')[-1].startswith('.'):
21 continue
22 file_path_list.extend([ os.path.join(path, file) for file in files if file.endswith(fileEx)])
23
24 file_path_list.sort()
25
26 file_name_list = []
27 for path in file_path_list:
28 file_name_list.append(os.path.basename(path))
29
30 return file_name_list, file_path_list
31
32def search_folder(folder):
33 """
34 폴더내부에 있는 폴더를 검색하는 기능
35 args:
36 folder (str): 검색하려는 폴더
37 return:
38 sub_folder_name_list (list[str]): 폴더명 리스트
39 sub_folder_path_list (list[str]): 폴더 경로 리스트
40 """
41 sub_folder_path_list = []
42 sub_folder_name_list = []
43 for item in os.listdir(folder): # 해당 폴더 내 모든 파일 및 폴더 추출
44 sub_folder = os.path.join(folder, item)
45
46 if os.path.isdir(sub_folder): # 폴더 여부 확인
47 sub_folder_path_list.append(sub_folder)
48 sub_folder_name_list.append(item)
49
50 return sub_folder_name_list, sub_folder_path_list
51
52
53def create_directory(directory):
54 """
55 디렉토리를 생성하는 기능
56 하위 디렉토리를 모두 생성함
57 args:
58 directory (str): 생성하려는 디렉토리
59 """
60 try:
61 if not os.path.exists(directory):
62 os.makedirs(directory)
63 else:
64 pass
65 except OSError:
66 print("Error: Failed to create the directory.")
67
68
69
70
71class JsonEncoder(json.JSONEncoder):
72 def default(self, obj):
73 if isinstance(obj, np.integer):
74 return int(obj)
75 elif isinstance(obj, np.floating):
76 return float(obj)
77 elif isinstance(obj, np.ndarray):
78 return obj.tolist()
79 else:
80 return super(JsonEncoder, self).default(obj)
81
82def save_json(data, path):
83 """
84 json 파일을 저장하는 기능
85 args:
86 data (dict): 데이터
87 path (str): 저장 경로
88 """
89 try:
90 with open(path, "w") as json_file:
91 json.dump(data, json_file)
92 except Exception as e:
93 print(e)
94
95def load_json(path):
96 """
97 json 파일을 로드하는 기능
98 args:
99 path (str): json 파일 경로
100 return (dict): json 파일의 데이터
101 """
102 try:
103 with open(path, "r") as json_file:
104 data = json.load(json_file)
105 except Exception as e:
106 data = None
107
108 return data
109
110def copy_file(src_path, dst_path):
111 """
112 파일을 복사하는 기능
113 args:
114 src_path (str): 복사하려는 파일 경로
115 dst_path (str): 복사한 파일을 저장하는 경로
116 """
117 try:
118 shutil.copy(src_path, dst_path)
119 except Exception as e:
120 print(f"파일 복사 중 오류 발생: {src_path} -> {dst_path}")
json 저장할 때 필요한 클래스
Definition utils_os.py:71
default(self, obj)
Definition utils_os.py:72
search_folder(folder)
Definition utils_os.py:32
load_json(path)
Definition utils_os.py:95
search_file(folder, fileEx)
Definition utils_os.py:6
copy_file(src_path, dst_path)
Definition utils_os.py:110
create_directory(directory)
Definition utils_os.py:53
save_json(data, path)
Definition utils_os.py:82