Dec-31-2018, 11:02 PM
(This post was last modified: Dec-31-2018, 11:02 PM by Gribouillis.)
It's a great idea. I suggest a context manager and a 'commit' keyword argument to the
.close()
method in order to have code such aswith skap_open("critical.cfg", "w") as ofile: ofile.write("frac = {}\n".format(x / y)) ofile.write("more data\n") ofile.close(commit=True)If we reach the
ofile.close(commit=True)
statement, critical.cfg is written. If we exit through an exception or some other way, the temporary file is destroyed but critical.cfg is left untouched. The commit keyword defaults to False, so that exiting the context defaults to not overwriting 'critical.cfg'.