-
Notifications
You must be signed in to change notification settings - Fork 9
/
file_manager.py
41 lines (32 loc) · 1.17 KB
/
file_manager.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
import json
import os
import pickle
def load(path, default):
try:
with open(path, "rb") as f:
print("file_manager.py: load: loading object from file: {}".format(path))
result = pickle.load(f)
except Exception:
result = default
# If wanting to auto write default file
# with open(path, "wb") as f:
# print(
# "file_manager.py: load: initializing new object to path: {}".format(path))
# pickle.dump(result, f)
return result
def save(object, path):
with open(path, 'wb') as output: # Overwrites any existing file.
print("file_manager.py: saved object to path: {}".format(path))
pickle.dump(object, output, pickle.HIGHEST_PROTOCOL)
def loadJSON(path, default):
try:
with open(path, "rb") as data:
print("file_manager.py: loadJSON: loading JSON from file: {}".format(path))
result = json.load(data)
except Exception:
result = default
return result
def saveJSON(object, path):
with open(path, 'wb') as f:
print("file_manager.py: saved JSON object to path: {}".format(path))
json.dump(object, f)