-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_utils.py
46 lines (33 loc) · 1.2 KB
/
file_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
################################################################################
# CSE 253: Programming Assignment 4
# Code snippet by Ajit Kumar, Savyasachi
# Fall 2020
################################################################################
import os
import json
def read_file(path):
if os.path.isfile(path):
with open(path) as json_file:
data = json.load(json_file)
return data
else:
raise Exception("file doesn't exist: ", path)
def read_file_in_dir(root_dir, file_name):
path = os.path.join(root_dir, file_name)
return read_file(path)
def delete_expr_folder(root_dir, expr_name):
path = os.path.join(root_dir, 'experiment_data/'+expr_name)
if os.path.isdir(path):
os.rmdir(path)
def write_to_file(path, data):
with open(path, "w") as outfile:
json.dump(data, outfile)
def write_to_file_in_dir(root_dir, file_name, data):
path = os.path.join(root_dir, file_name)
write_to_file(path, data)
def log_to_file(path, log_str):
with open(path, 'a') as f:
f.write(log_str + '\n')
def log_to_file_in_dir(root_dir, file_name, log_str):
path = os.path.join(root_dir, file_name)
log_to_file(path, log_str)