-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreporting.py
More file actions
24 lines (18 loc) · 786 Bytes
/
Copy pathreporting.py
File metadata and controls
24 lines (18 loc) · 786 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import json
import os
# Function to append/update JSON file with new dictionary
def append_to_json(file_path, new_data):
# Initialize data structure as an empty dictionary if file doesn't exist
data = {}
# If the file exists, load its content into `data`
if os.path.exists(file_path):
with open(file_path, "r") as file:
data = json.load(file)
# Use the "name" entry from `new_data` as the key
key = new_data["name"]
# Append new_data to the existing data, or create a new entry if it doesn't exist
# If the key already exists, this will overwrite the existing data for that key
data[key] = new_data
# Write the updated data back to the file
with open(file_path, "w") as file:
json.dump(data, file, indent=4)