-
Notifications
You must be signed in to change notification settings - Fork 411
/
utils.py
46 lines (34 loc) · 1.29 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
# -*- coding: utf-8 -*-
"""
Created on Fri Nov 1 10:28:41 2019
@author: rocheng
"""
import os
import csv
from shutil import copyfile
import glob
def get_dir(cfg, param_name, new_dir_name):
'''Helper function to retrieve directory name if it exists,
create it if it doesn't exist'''
if param_name in cfg:
dir_name = cfg[param_name]
else:
dir_name = os.path.join(os.path.dirname(__file__), new_dir_name)
if not os.path.exists(dir_name):
os.makedirs(dir_name)
return dir_name
def write_log_file(log_dir, log_filename, data):
'''Helper function to write log file'''
data = zip(*data)
with open(os.path.join(log_dir, log_filename), mode='w', newline='') as csvfile:
csvwriter = csv.writer(csvfile, delimiter=' ',
quotechar='|', quoting=csv.QUOTE_MINIMAL)
for row in data:
csvwriter.writerow([row])
def str2bool(string):
return string.lower() in ("yes", "true", "t", "1")
def rename_copyfile(src_path, dest_dir, prefix='', ext='*.wav'):
srcfiles = glob.glob(f"{src_path}/"+ext)
for i in range(len(srcfiles)):
dest_path = os.path.join(dest_dir, prefix+'_'+os.path.basename(srcfiles[i]))
copyfile(srcfiles[i], dest_path)