Question, I have an application that accesses data in a json file. Right now, every time the application needs the data, I will open and close the file as such.
def access_file():
try:
with open(my_file, 'r') as json_data:
json_data = json.load(json_data)
return json_data
except FileNotFoundError:
logging.error("my_file not found.")
I am assuming it is not smart to continually be opening and closing this file so many times. How do I only open it once if it's not open and keep it open, then I can just access in memory if I need it.
json_datacan be returned, everything is fine.