-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_manager.py
More file actions
23 lines (19 loc) · 841 Bytes
/
Copy pathfile_manager.py
File metadata and controls
23 lines (19 loc) · 841 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import json
class FileManager:
def __init__(self):
"""creates an empty json file '[]'
if exists deletes it's content
"""
with open("files/data.json", "w", encoding='utf8') as json_file:
json.dump(json.loads('[]'), json_file, indent=4)
def write_file(self, dictionary: dict) -> None:
"""Saves one job offer at a time"""
with open("files/data.json", "r+", encoding='utf8') as json_file:
# First we load existing data into a dict.
file_data: list = json.load(json_file)
# Join new_data with file_data
file_data.append(dictionary)
# Sets file's current position at offset.
json_file.seek(0)
# convert back to json.
json.dump(file_data, json_file, indent=4, ensure_ascii=False)