-
Notifications
You must be signed in to change notification settings - Fork 0
/
file_storage.py
50 lines (42 loc) · 1.42 KB
/
file_storage.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
42
43
44
45
46
47
48
49
50
#!/usr/bin/python3
"""
Module Name:
file_storage
Module Description:
This module contains one class that manage the file storage
Module Classes:
- FileStorage
Module Attributes:
- None
"""
import json
from os import path
class FileStorage:
"""
The FileStorage class is used to save and load data from a JSON file.
"""
def __init__(self, file_path="storage.json"):
"""
The FileStorage class has a constructor that takes a file_path argument.
If no argument is provided, it defaults to "storage.json". The file_path
parameter is used to specify the location of the JSON file to be read from and written to.
"""
self.file_path = file_path
def save(self, data: dict) -> None:
"""
The save method writes a Python dictionary to the JSON file specified by the
file_path parameter. The data will be written in an indented JSON format.
"""
with open(self.file_path, 'w') as f:
json.dump(data, f, indent=4)
def reload(self) -> dict:
"""
The reload method reads a JSON file specified by the file_path parameter and
returns its content as a Python dictionary. If the file does not exist, it returns None.
"""
if path.exists(self.file_path):
with open(self.file_path, 'r') as f:
data = json.load(f)
return data
else:
return None