Add read_history function to read history from history.json

This commit is contained in:
Sirin Puenggun 2022-12-11 13:56:00 +07:00
parent c143c0ca2f
commit 9dd0dec3f9

View File

@ -1,4 +1,5 @@
import json
from json import JSONDecodeError
import os
def write_command(com, inp, out) -> None:
@ -41,4 +42,16 @@ def delete() -> bool:
os.remove("history.json")
return True
except:
return False
return False
def read_history():
filename = "history.json"
try:
with open(filename, "r") as file:
number_dict = json.load(file)
for line, data in number_dict.items():
print(f" ({line}) Command: {data['command']} | Input: {data['input']} | Output: {data['output']}")
except FileNotFoundError:
raise FileNotFoundError("History file not found! Maybe program hasn't been saved yet.")
except JSONDecodeError:
print("History is empty.")