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<div><br></div><div>if not os.path.exists(dst):</div><div>
    shutil.move(src, dst)</div><div><br></div><div>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 <a href="http://bytes.com/topic/python/answers/555794-safely-renaming-file-without-overwriting">http://bytes.com/topic/python/answers/555794-safely-renaming-file-without-overwriting</a>).  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:</div>
<div><br></div><div><div>def move2(src, dst):</div><div>    try:</div><div>        fd = os.open(dst, os.O_EXCL | os.O_CREAT)</div><div>    except OSError:</div><div>        raise Error('Destination exists')</div><div>
    try:</div><div>        move(src, dst)</div><div>    finally:</div><div>        os.close(fd)</div></div><div><br></div><div>This could be optimised by using shutil.move code rather than just calling it, but the idea is that an attempt is made to create dst with exclusive access. If this fails, then it means that the file exists, but if it passes, then dst is locked so no other process can create it.</div>