forked from umautobots/SAD-GS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
root_file_io.py
354 lines (291 loc) · 10.1 KB
/
root_file_io.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import inspect
import csv
import os
import shutil
import random
import string
from PIL import Image
import numpy as np
import pandas as pd
from distutils.dir_util import copy_tree
'''
1. Root directories, and image extensions of the whole project
'''
sep = os.sep
img_ext_set = ['jpg', 'jpeg', 'png', 'JPG', 'JPEG', 'PNG', 'bmp', 'BMP']
'''
2. File extension
'''
def get_filename_components(file_path):
file = ''
ext = ''
file_dir = ''
(file_dir, file) = os.path.split(file_path)
name_combo = str(file).split('.')
if len(name_combo) >= 2:
file = name_combo[0]
ext = name_combo[-1]
elif len(name_combo) == 1:
file = name_combo[0]
ext = str(ext).replace('.', '')
return (file_dir, file, ext)
def filter_ext(filepath_list, filter_out_target=False, ext_set=None):
if ext_set is None:
return
unwanted_elems = list()
for pi_path in filepath_list:
(file_dir, file, ext) = get_filename_components(pi_path)
target_exist = False
if (ext in ext_set) and len(ext) > 0:
target_exist = True
if (target_exist == True) and (filter_out_target == True):
unwanted_elems.append(pi_path)
elif (target_exist == False) and (filter_out_target == False):
unwanted_elems.append(pi_path)
return [ele for ele in filepath_list if ele not in unwanted_elems]
def filter_folder(fid_folder_list, filter_out=False, filter_text=''):
rslt = []
for ff in fid_folder_list:
if (filter_text in ff) and (filter_out == False):
rslt.append(ff)
elif (not (filter_text in ff)) and (filter_out == True):
rslt.append(ff)
return rslt
def filter_if_dir(filepath_list, filter_out_target=False):
# if isinstance(directory, list):
rslt = []
for ff in filepath_list:
if file_exist(ff) == False:
continue
if (os.path.isdir(ff)) and (filter_out_target == False):
# need to return folder
rslt.append(ff)
elif (os.path.isfile(ff)) and (filter_out_target == True):
# need to return only files
rslt.append(ff)
return rslt
def replace_file_ext(filepath, new_ext, full_path=True, replace_save=False):
if (full_path):
(filedir, file, ext) = get_filename_components(filepath)
new_file = filedir + sep + file + '.' + new_ext
if replace_save:
move_file(filepath, new_file)
return new_file
else:
(file, ext) = str(filepath).split('.')
new_file = file + '.' + new_ext
if replace_save:
move_file(filepath, new_file)
return new_file
'''
3. File/folder path
'''
def createPath(separator, list_of_dir, file_name=""):
if len(list_of_dir) <= 0:
return ""
while '' in list_of_dir:
list_of_dir.remove('')
path_rslt = separator.join(list_of_dir)
if len(file_name) <= 0:
return path_rslt
else:
return path_rslt + separator + file_name
def getParentDir():
current_path = os.path.dirname(os.path.abspath('__file__'))
return current_path
def getGrandParentDir():
current_path = os.path.dirname(os.path.abspath('__file__'))
return os.path.abspath(os.path.join(current_path,os.path.pardir))
def traverse_dir(dir, full_path=False, towards_sub=False):
rslt = list()
if towards_sub == False:
file_list = os.listdir(dir)
for file_name in file_list:
if full_path:
rslt.append(os.path.join(dir, file_name))
else:
rslt.append(file_name)
return rslt
else:
g = os.walk(dir)
for path, dir_list, file_list in g:
for file_name in file_list:
if full_path:
rslt.append(os.path.join(path, file_name))
else:
rslt.append(file_name)
return rslt
def get_nextsub_from_dir_list(dir_list, full_path=False):
rslt = {}
for dir in dir_list:
if '.DS_Store' in dir:
continue
sub_list = traverse_dir(dir, full_path=full_path)
sub_list = filter_ext(sub_list, filter_out_target=True, ext_set='.DS_Store')
rslt[dir] = sub_list
return rslt
def file_exist(file_path):
if 'NA' == file_path:
return False
if (file_path == file_path) == False:
return False
return os.path.exists(file_path)
def check_file_permission(file_path, destination_path):
if os.access(file_path, os.R_OK) or os.access(destination_path, os.W_OK):
return True
else:
# print("Permission denied for file movement.", file_path)
return False
'''
4. File/folder copy, paste, delete and create
'''
def ensure_dir(directory):
try:
if isinstance(directory, list):
directory = createPath(sep, directory)
if not os.path.exists(directory):
os.makedirs(directory)
except:
print("FATAL ERROR: ENSURE_DIR in file_io.py!")
def delete_file(path):
if os.path.exists(path): #if file exists
os.remove(path)
else:
print('no such file:%s' % path)
def delete_folder(path):
if os.path.exists(path):
if len(os.listdir(path)) == 0:
os.removedirs(path)
else:
try:
shutil.rmtree(path)
except OSError as e:
print("Error: %s - %s." % (e.filename, e.strerror))
else:
print('no such folder:%s' % path)
def move_file(from_dir, to_dir, required_ext=''):
try:
if len(required_ext) < 1:
shutil.move(from_dir, to_dir)
else:
cp_func = '*.' + required_ext
shutil.move(from_dir, to_dir, cp_func)
except Exception as e:
print('ERROR: ', e)
def copy_file(from_dir, to_dir, required_ext=''):
if len(required_ext) < 1:
shutil.copy(from_dir, to_dir)
else:
cp_func = '*.' + required_ext
shutil.copy(from_dir, to_dir, cp_func)
def copy_folder(from_dir, to_dir):
copy_tree(from_dir, to_dir)
'''
5. File savings and readings: to csv
'''
def save_df_to_csv(rslt_df, file_path, mode='a+', encode='utf_8', breakline='', write_head=True):
try:
header = list(rslt_df.head())
with open(file_path, mode, encoding=encode, newline=breakline) as f:
writer = csv.writer(f)
if write_head:
header = list(rslt_df.head())
writer.writerow(header)
for index, row in rslt_df.iterrows():
writer.writerow(row)
f.close()
except Exception as e:
print("write error==>", e)
pass
def save_dict_to_csv(dict_to_save, file_path, mode='a', encode='utf_8', breakline=''):
keyword_list = dict_to_save.keys()
try:
if not os.path.exists(file_path):
with open(file_path, "w", newline='', encoding='utf-8') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=keyword_list)
writer.writeheader()
with open(file_path, mode=mode, newline=breakline, encoding=encode) as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=keyword_list)
writer.writerow(dict_to_save)
except Exception as e:
print("write error==>", e)
pass
def save_list_to_csv(list_to_save, file_path, mode='a', encode='utf_8'):
try:
with open(file_path, mode, encoding=encode, newline='') as f:
write = csv.writer(f)
rows = [[data] for data in list_to_save]
write.writerows(rows)
except Exception as e:
print("write error==>", e)
pass
'''
6. File savings and readings: to txt
'''
def save_dict_to_txt(dictionary, filename, mode='w', encode='utf_8'):
try:
f = open(filename, mode, encoding=encode)
f.write(str(dictionary))
f.close()
except Exception as e:
print("write error==>", e)
pass
def read_dict_from_txt(filename, mode='r', encode='utf_8'):
try:
f = open(filename, mode, encoding=encode)
a = f.read()
dictionary = eval(a)
f.close()
return dictionary
except Exception as e:
print("write error==>", e)
pass
def save_str_to_txt(str_to_save, file_path, mode='a', encode='utf_8', breakline=''):
try:
if not os.path.exists(file_path):
with open(file_path, "w", newline='', encoding='utf-8') as f:
f.write(str_to_save)
else:
with open(file_path, mode=mode, newline=breakline, encoding=encode) as f:
f.write(str_to_save)
except Exception as e:
print("write error==>", e)
pass
def save_list_to_txt(list_to_save, file_path, mode='a', encode='utf_8'):
try:
with open(file_path, mode, encoding=encode, newline='') as f:
for item in list_to_save:
list_to_save.write(f"\n{item}")
except Exception as e:
print("write error save_list_to_txt ==>", e)
pass
def read_list_from_txt(filename, mode='r', encode='utf_8'):
try:
with open(filename, mode, encoding=encode) as f:
lines = f.readlines()
newlines =[x.strip() for x in lines]
return newlines
except Exception as e:
print("read error save_list_to_txt ==>", e)
pass
'''
7. File Conversions
'''
def image_to_dataframe(image_path):
frame = inspect.currentframe()
print('Running func: {} -- {}', inspect.getframeinfo(frame).function, image_path)
colourImg = Image.open(image_path)
colourPixels = colourImg.convert("RGB")
colourArray = np.array(colourPixels.getdata()).reshape(colourImg.size + (3,))
indicesArray = np.moveaxis(np.indices(colourImg.size), 0, 2)
allArray = np.dstack((indicesArray, colourArray)).reshape((-1, 5))
source_df = pd.DataFrame(allArray, columns=["y", "x", "R", "G", "B"])
return source_df
'''
Other File Operations
'''
def generate_random_string(rs_len=8):
letters = string.ascii_letters
result_str = ''.join(random.choice(letters) for i in range(rs_len))
print("Random string of length", rs_len, "is:", result_str)
return result_str