[Python-ideas] Rewriting file - pythonic way

Mahmoud Hashemi mahmoud at hatnote.com
Sun Apr 15 12:10:57 EDT 2018


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.

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.
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180415/626360e0/attachment.html>


More information about the Python-ideas mailing list