Safemotion Lib
Loading...
Searching...
No Matches
Classes | Functions
utils_os Namespace Reference

Classes

class  JsonEncoder
 json 저장할 때 필요한 클래스 More...
 
class  MyEncoder
 

Functions

 save_keep_index (sorted_keep_index, crop_image_path, test_image_list, save_path)
 
 search_file (folder, fileEx)
 
 search_folder (folder)
 
 create_directory (directory)
 
 save_json (data, path)
 
 load_json (path)
 
 copy_file (src_path, dst_path)
 

Function Documentation

◆ copy_file()

utils_os.copy_file ( src_path,
dst_path )
파일을 복사하는 기능
args:
    src_path (str): 복사하려는 파일 경로
    dst_path (str): 복사한 파일을 저장하는 경로

Definition at line 110 of file utils_os.py.

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}")

◆ create_directory()

utils_os.create_directory ( directory)
디렉토리를 생성하는 기능
하위 디렉토리를 모두 생성함
args:
    directory (str): 생성하려는 디렉토리

Definition at line 53 of file utils_os.py.

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

◆ load_json()

utils_os.load_json ( path)
json 파일을 로드하는 기능
args:
    path (str): json 파일 경로
return (dict): json 파일의 데이터

Definition at line 95 of file utils_os.py.

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

◆ save_json()

utils_os.save_json ( data,
path )
json 파일을 저장하는 기능
args:
    data (dict): 데이터
    path (str): 저장 경로

Definition at line 82 of file utils_os.py.

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

◆ save_keep_index()

utils_os.save_keep_index ( sorted_keep_index,
crop_image_path,
test_image_list,
save_path )

Definition at line 22 of file utils_os.py.

22def save_keep_index(sorted_keep_index, crop_image_path, test_image_list, save_path):
23 save_file = os.path.join(save_path,'keep_index.json')
24 data = {}
25 data['image_path'] = []
26 data['s_sim'] = []
27 for index in sorted_keep_index:
28 image_path = os.path.join(crop_image_path, test_image_list[index[0]])
29 data['image_path'].append(image_path)
30 data['s_sim'].append(index[1])
31 print(f'image_path = {image_path}')
32 print(f's_sim = {index[1]}')
33
34 with open(save_file, 'w') as outfile:
35 json.dump(data, outfile,cls=MyEncoder)

◆ search_file()

utils_os.search_file ( folder,
fileEx )
폴더에서 특정 확장자의 파일을 검색하는 기능
하위 폴더까지 모두 검색
args:
    folder (str): 검색하려는 폴더
    fileEx (str): 파일 확장자
return:
    file_name_list (list[str]): 파일 이름 리스트
    file_path_list (list[str]): 파일 경로 리스트

Definition at line 6 of file utils_os.py.

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

◆ search_folder()

utils_os.search_folder ( folder)
폴더내부에 있는 폴더를 검색하는 기능
args:
    folder (str): 검색하려는 폴더
return:
    sub_folder_name_list (list[str]): 폴더명 리스트
    sub_folder_path_list (list[str]): 폴더 경로 리스트

Definition at line 32 of file utils_os.py.

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