[Python-ideas] Rewriting file - pythonic way
Paul Moore
p.f.moore at gmail.com
Sun Apr 15 06:47:17 EDT 2018
On 15 April 2018 at 11:22, Elazar <elazarg at gmail.com> wrote:
> בתאריך יום א׳, 15 באפר׳ 2018, 13:13, מאת Serhiy Storchaka
> <storchaka at gmail.com>:
>> Actually the reliable code should write into a separate file and replace
>> the original file by the new file only if writing is successful. Or
>> backup the old file and restore it if writing is failed. Or do both. And
>> handle hard and soft links if necessary. And use file locks if needed to
>> prevent race condition when read/write by different processes. Depending
>> on the specific of the application you may need different code. Your
>> three lines are enough for a one-time script if the risk of a powerful
>> blackout or disk space exhaustion is insignificant or if the data is not
>> critical.
>
> This pitfall sounds like a good reason to have such a function in the
> standard library.
It certainly sounds like a good reason for someone to write a "safe
file rewrite" library function. But I don't think that it's such a
common need that it needs to be a stdlib function. It may well even be
the case that there's such a function already available on PyPI - has
anyone actually checked? And if there isn't, then writing module and
publishing it there would seem like a *very* good starting point - as
well as allowing the developer to thrash out the best API, it would
also provide for lots of testing in unusual scenarios that the
developer may not have thought about (Windows file locking is very
different from Unix, what is an atomic operation differs between
platforms, error handling and retries may be something to consider,
etc).
The result would be a useful package, and the download and activity
stats for it would be a great indication of whether it's a frequent
enough need to justify including in core Python.
IMO, it probably isn't. I suspect that most uses would be fine with
the quoted 3-liner, but very few people would need the sort of
robustness that Serhiy is describing (and that level of robustness
*would* be needed for a stdlib implementation). So PyPI is likely a
better home for the "bulletproof" version, and 3 lines of code is a
perfectly acceptable and Pythonic solution for people with simpler
needs.
Paul
More information about the Python-ideas
mailing list