Safely renaming a file without overwriting

Steven D'Aprano steve at
Sun Oct 29 00:15:24 CEST 2006

On Sat, 28 Oct 2006 13:38:14 +0200, Wolfgang Draxinger wrote:

>> But on a multi-user system, it is possible that dest is created
>> in the time period between checking if it exists and attempting
>> the rename.
>> Is there any way to prevent this? Or do I just try to keep the
>> check and the rename as close together as possible, minimizing
>> the chances and hoping for the best?
> 1: Open the file with

Open "the" file? There are potentially two files -- the source and
destination. I only want to do the rename if the destination
*doesn't* exist, so there is no destination file to open. How will it help
me to lock the source file? Have I misunderstood?


More information about the Python-list mailing list