I have a file that may or may not be empty. The goal is to read from the file an json object, append to it, and then write it back to the file. However, on the case that the file is empty, then json.load() fails. How do I resolve this without having to check if the file is empty beforehand?
Here is my current code:
with open(filename, 'a+') as infile:
    old_data = json.load(infile)
data = old_data + obj
with open(filename, 'w') as outfile:
    json.dump(data, outfile)
Error Message is:
 File "<stdin>", line 3, in <module>
  File "/usr/lib/python3.6/json/__init__.py", line 299, in load
    parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
  File "/usr/lib/python3.6/json/__init__.py", line 354, in loads
    return _default_decoder.decode(s)
  File "/usr/lib/python3.6/json/decoder.py", line 339, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "/usr/lib/python3.6/json/decoder.py", line 357, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
 
     
    