-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_handler.py
More file actions
56 lines (43 loc) · 1.43 KB
/
file_handler.py
File metadata and controls
56 lines (43 loc) · 1.43 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
import json
class File_Handler:
def __init__(self,file_name):
self.file_name = file_name
def create_new_file(self):
with open(self.file_name, 'w') as f:
json.dump({'index': 0,'sender': 'server', 'message': ""},f)
f.write('\n')
def empty_file(self):
with open(self.file_name, 'w') as f:
pass
def append_json_data(self, json_data):
with open(self.file_name, 'a') as f:
json.dump(json_data, f)
f.write('\n')
def read_json_array(self):
with open(self.file_name, 'r') as f:
json_lines = f.readlines()
json_array = []
for line in json_lines:
json_data = json.loads(line)
json_array.append(json_data)
return json_array
def get_last_json(self):
data = self.read_json_array()
last = len(data)
return data[last-1]
def del_json_with_index(self,ind):
data = self.read_json_array()
newData =[]
row = 0
for d in data:
if(int(d['index']) == ind):
pass
else:
newData.append({"index": row, "sender": d['sender'], "message": d['message']})
row += 1
self.empty_file()
for d in newData:
self.append_json_data(d)
def get_len_of_json_array(self):
data = self.read_json_array()
return len(data)