os independent rename

Dave Angel davea at ieee.org
Sat Sep 17 20:04:42 EDT 2011


On 01/-10/-28163 02:59 PM, Nobody wrote:
> On Sat, 17 Sep 2011 20:28:32 +0430, Lee Harr wrote:
>
>> So, what is the best way to do this that will
>> behave the same across operating systems?
> Delete the destination first, but after checking that it isn't the same as
> the source.
>
> On Windows, that last bit is harder than it seems. A string-based
> comparison won't work. Even if you ignore case, there's the issue of 8.3
> filenames, and some other odd cases.
>
>
Or rename it twice, with the intermediate file being something that does 
not exist.  For example, generate a random name, and if it exists, 
repeat till you come up with one that does not.  Once the first rename 
has succeeded, you can safely delete the destination.

Even this approach can have problems in the face of symlinks or multiple 
partitions.

DaveA




More information about the Python-list mailing list