[Python-ideas] Implementation of shutil.move

Antoine Pitrou solipsis at pitrou.net
Fri Aug 12 14:29:21 CEST 2011


On Fri, 12 Aug 2011 13:59:22 +0200
David Townshend <aquavitae69 at gmail.com>
wrote:
> The shutil.move function uses os.rename to move files on the same file
> system. On unix, this function will overwrite an existing destination, so
> the obvious approach is
> 
> if not os.path.exists(dst):
>     shutil.move(src, dst)
> 
> But this could result in race conditions if dst is created after
> os.path.exists and before shutil.move.  From my research, it seems that this
> is a limitation in the unix c library, but it should be possible to avoid it
> through a workaround (pieced together from
> http://bytes.com/topic/python/answers/555794-safely-renaming-file-without-overwriting).
>  This involves some fairly low-level work, so I propose adding a new move2
> function to shutil, which raises an error if dst exists and locking it if it
> doesn't:

This is a reasonable request (although it could also be an optional
argument to shutil.move() rather than a separate function).
Could you open an issue with your proposal on http://bugs.python.org ?
You are also welcome to submit a patch in the issue; please see
http://docs.python.org/devguide/

Regards

Antoine.





More information about the Python-ideas mailing list