[Python-ideas] Rewriting file - pythonic way
Serhiy Storchaka
storchaka at gmail.com
Sun Apr 15 06:12:13 EDT 2018
15.04.18 12:49, Alexey Shrub пише:
> В Воскресенье, 15 апр. 2018 в 12:40 , Serhiy Storchaka
> <storchaka at gmail.com> написал:
>> If the problem is that you want to use a single line instead of three
>> line, you can add a function
>
> Yes, I think that single line with word 'rewrite' is much more readable
> than those three lines.
> And yes, I can make my own function, but it is typical task - maybe it
> must be in standard library?
Not every three lines of code must be a function in standard library.
And these three lines don't look enough common.
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.
More information about the Python-ideas
mailing list