[Python-ideas] Rewriting file - pythonic way

Oleg Broytman phd at phdru.name
Sun Apr 15 12:49:16 EDT 2018


On Sun, Apr 15, 2018 at 09:10:57AM -0700, Mahmoud Hashemi <mahmoud at hatnote.com> wrote:
> Depending on how firm your requirements around locking are, you may find
> this code useful:
> https://github.com/mahmoud/boltons/blob/6b0721b6aeda6d3ec6f5d31be7c741bc7fcc4635/boltons/fileutils.py#L303
> 
> (docs here:
> http://boltons.readthedocs.io/en/latest/fileutils.html#atomic-file-saving )
> 
> Basically every operating system has _some_ way of doing an atomic file
> replacement, letting us guarantee that a file at a given location is always
> valid. atomic_save provides a unified interface to that cross-platform
> behavior.
> 
> The code does not do locking, as neither I nor its other users have wanted
> it, but I'd be happy to extend it if there's a sensible default.

   I don't like it renames the file at the end. Renaming could lead to
changed file ownership and permissions; restoring permissions is not
always possible, restoring ownership is almost never possible. Renaming
is also not always possible due to restricted directory permissions.

> On Sun, Apr 15, 2018 at 8:19 AM, Oleg Broytman <phd at phdru.name> wrote:
> 
> > On Sun, Apr 15, 2018 at 05:15:55PM +0300, Alexey Shrub <ashrub at yandex.ru>
> > wrote:
> > > В Воскресенье, 15 апр. 2018 в 2:40 , Nick Coghlan <ncoghlan at gmail.com>
> > > написал:
> > > > https://bugs.python.org/issue8604#msg174104 is the relevant tracker
> > > > discussion
> > >
> > > Thanks all, I agree that universal and absolutly safe solution is very
> > > difficult, but for experiment I made some draft
> > > https://github.com/worldmind/scripts/tree/master/filerewrite
> >
> >    Good!
> >
> > > main code here
> > > https://github.com/worldmind/scripts/blob/master/
> > filerewrite/filerewrite.py#L46
> >
> >    Can I recommend to catch exceptions in `backuper.backup()`,
> > cleanup backuper and unlock locker?


Oleg.
-- 
     Oleg Broytman            http://phdru.name/            phd at phdru.name
           Programmers don't die, they just GOSUB without RETURN.


More information about the Python-ideas mailing list