The following code throws
TypeError: Object of type 'datetime' is not JSON serializable
which I know how to resolve.  However my real question is how to cleanly structure the code to avoid a partial file if any exception occurs in json.dump.
import datetime
import json
def save(data):
    with open('data.txt', 'w') as outfile:
        json.dump(data, outfile)
data = dict(sometime=datetime.datetime.now())
save(data)
The above code throws an exception and results in a partial file like:
{"sometime": 
Should I dumps to a string first in a try/except?  If so are there any memory implications to be aware of? Or delete the file in an except block?
 
     
    