-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileUtil.py
More file actions
83 lines (57 loc) · 1.92 KB
/
FileUtil.py
File metadata and controls
83 lines (57 loc) · 1.92 KB
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
import re
import json
def write_json_file(json_data, json_file):
"""
Writes dictionary into json file
:param json_data: dictionary to write
:param json_file: file to dump json data into
"""
print 'Writing - ', json_file
with open(json_file, 'w') as out_file:
json.dump(json_data, out_file)
def read_json_file(json_file):
"""
Returns dictionary containing contents of given json file
:param json_file: json file containing json data
:return: dictionary containing json data
"""
print 'Reading - ', json_file
with open(json_file, 'r') as read_file:
data = json.load(read_file)
return data
def list_to_file(write_list, write_file):
"""
Writes given list to a file as each list element in one line
:param write_list: list to write
:param write_file: destination file
"""
with open(write_file, 'w') as out_file:
for element in write_list:
out_line = "{}\n".format(element)
out_file.write(out_line)
def read_list_into_list(read_file):
"""
Reads each line into file in an array where each elements in
the array is an array of space separated elements in each line
:param read_file: file to read
:return: the list read from file
"""
data_array = []
with open(read_file, 'r') as in_file:
lines = in_file.read().splitlines()
for line in lines:
split_array = re.split('\\s+', line.strip())
data_array.append(split_array)
return data_array
def read_word_into_list(read_file):
"""
Reads each line into file in an array where each elements
:param read_file: file to read
:return: the list read from file
"""
data_array = []
with open(read_file, 'r') as in_file:
lines = in_file.read().splitlines()
for line in lines:
data_array.append(line.strip())
return data_array