forked from Queuecumber/828j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
61 lines (47 loc) · 1.71 KB
/
utils.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
import os
import pickle
import file_constants as file_const
def dataset_tuples(dataset_path):
return dataset_path + '_tuples_class'
def get_dataset_path(dataset_name):
for dir in file_const.dataset_path:
current_path = os.path.join(dir,dataset_name)
if(os.path.exists(current_path)):
return current_path;
return None
def get_dirs(base_path):
return sorted([f for f in os.listdir(base_path) if os.path.isdir(os.path.join(base_path, f))])
def get_files(base_path,extension,append_base=False):
if (append_base):
files =[os.path.join(base_path,f) for f in os.listdir(base_path) if (f.endswith(extension) and not f.startswith('.'))];
else:
files = [f for f in os.listdir(base_path) if (f.endswith(extension) and not f.startswith('.'))];
return sorted(files);
def txt_read(path):
with open(path) as f:
content = f.readlines()
lines = [x.strip() for x in content]
return lines;
def txt_write(path,lines):
out_file = open(path, "w")
for line in lines:
out_file.write(line)
out_file.write('\n')
out_file.close()
def pkl_write(path,data):
pickle.dump(data, open(path, "wb"))
def pkl_read(path):
if(not os.path.exists(path)):
return None;
data = pickle.load(open(path, 'rb'))
return data;
def touch_dir(path):
if(not os.path.exists(path)):
os.makedirs(path)
def last_tuple_idx(path):
files =[f for f in os.listdir(path) if (f.endswith('.jpg') and not f.startswith('.'))];
return len(files);
def get_file_name_ext(inputFilepath):
filename_w_ext = os.path.basename(inputFilepath)
filename, file_extension = os.path.splitext(filename_w_ext)
return filename, file_extension