add appendToJSON() function

This commit is contained in:
mst 2024-11-26 14:45:41 +03:00
parent 30f90da265
commit bbe8994907

View file

@ -30,6 +30,25 @@ def saveJSON(dict, file_path):
# dump the contents
json.dump(dict, file, indent=4)
# append to a json file
def appendToJSON(new_data, file_path):
try:
# open the file
path = Path(file_path)
if not path.is_file():
with open(path, 'w', encoding="utf-8") as file:
json.dump([], file)
with open(path, 'r+', encoding="utf-8") as file:
file_data = json.load(file)
file_data.append(new_data)
file.seek(0)
json.dump(file_data, file, indent=4)
return True
except Exception as e:
app.logger.error(str(e))
return False
cfg = loadJSON(const.configFile)
def formatRelativeTime(date_str):