I have a file that is updated with new content and saved every hour:
with open('data.txt','w') as outfile:
json.dump(data,outfile)
I then read this file at any given time:
with open('data.txt') as json_file:
data = json.load(json_file)
The problem I'm having is sometimes this file is in the process of being updated with new content when trying to read the file which results in a json.decoder.JSONDecodeError. How can I avoid this error? Maybe a try except case that waits for the file to be readable?