Top-rated recipes tagged "save" Code RecipesSafely and atomically write to a file (Python) 2016-03-23T14:14:26-07:00Steven D'Aprano <p style="color: grey"> Python recipe 579097 by <a href="/recipes/users/4172944/">Steven D'Aprano</a> (<a href="/recipes/tags/atomic/">atomic</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/save/">save</a>). Revision 3. </p> <p>Saving the user's data is risky. If you write to a file directly, and an error occurs during the write, you may corrupt the file and lose the user's data. One approach to prevent this is to write to a temporary file, then only when you know the file has been written successfully, over-write the original. This function returns a context manager which can make this more convenient.</p> <p>Update: this now uses <code>os.replace</code> when available, and hopefully will work better on Windows.</p> Write a PNG image in native Python (Python) 2010-10-28T12:03:52-07:00Campbell Barton <p style="color: grey"> Python recipe 577443 by <a href="/recipes/users/4168177/">Campbell Barton</a> (<a href="/recipes/tags/alpha/">alpha</a>, <a href="/recipes/tags/image/">image</a>, <a href="/recipes/tags/pil/">pil</a>, <a href="/recipes/tags/png/">png</a>, <a href="/recipes/tags/save/">save</a>). </p> <p>This is a simple PNG writing function, intended to be as small as possible. It only supports RGBA 32bit PNGs and the input should be a buffer of rgba bytes.</p> <p>Tested for python 2 and 3.</p> Wasted Money Calculator (Python) 2012-01-26T02:27:25-08:00userend <p style="color: grey"> Python recipe 578026 by <a href="/recipes/users/4179007/">userend</a> (<a href="/recipes/tags/calculate/">calculate</a>, <a href="/recipes/tags/calculator/">calculator</a>, <a href="/recipes/tags/math/">math</a>, <a href="/recipes/tags/money/">money</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/save/">save</a>, <a href="/recipes/tags/tkinter/">tkinter</a>). Revision 2. </p> <p>Calculate the amount of money you waste on the unnecessary items you buy everyday. </p>