os.rename copies when old is in-use - is this deliberate?

Bengt Richter bokr at oz.net
Sat Dec 3 12:32:06 CET 2005


On Sat, 3 Dec 2005 22:32:22 +1300, Tony Meyer <tony.meyer at gmail.com> wrote:

>On Windows, if I do os.rename(old, new) where old is a file that is
>in-use (e.g. python itself, or a dll that is loaded), I would expect
>that an error would be raised (e.g. as when os.remove is called with
>an in-use file).  However, what happens is that a copy of the file is
>made, and the old file still exists.
>
>For example:
>
>C:\>c:\python24\python.exe
>Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on wi=
>n32
>Type "help", "copyright", "credits" or "license" for more information.
>>>> import os
>>>> import sys
>>>> sys.executable
>'c:\\python24\\python.exe'
>>>> os.rename(sys.executable, "d:\\python24.exe")
>>>> ^Z
>
>D:\>dir c:\python24\p*.exe
> Volume in drive C is ACER
> Volume Serial Number is 320D-180E
>
> Directory of c:\python24
>
>28/09/2005  12:41 p.m.             4,608 python.exe
>28/09/2005  12:41 p.m.             5,120 pythonw.exe
>               2 File(s)          9,728 bytes
>               0 Dir(s)  16,018,685,952 bytes free
>
>C:\>dir d:\p*24.exe
> Volume in drive D is DATA
> Volume Serial Number is 4019-78E0
>
> Directory of d:\
>
>28/09/2005  12:41 p.m.             4,608 python24.exe
>               1 File(s)          4,608 bytes
>               0 Dir(s)  15,362,207,744 bytes free
>
>Is this the intended behaviour?  The documentation doesn't give any
>indication that it is (so unless I'm missing something, this is at
>least a documentation bug).
>
>Any insight appreciated :)  (I do know that I can work around it by
>doing a remove after the rename, if the file exists).
>
Seems like os.rename may be trying to be too helpful

(windows NT4)
 [ 3:26] C:\pywk\grammar>help ren
 Renames a file or files.

 RENAME [drive:][path]filename1 filename2.
 REN [drive:][path]filename1 filename2.

 Note that you cannot specify a new drive or path for your destination file.

 [ 3:26] C:\pywk\grammar>py24
 Python 2.4b1 (#56, Nov  3 2004, 01:47:27)
 [GCC 3.2.3 (mingw special 20030504-1)] on win32
 Type "help", "copyright", "credits" or "license" for more information.
 >>> import os
 >>> help(os.rename)
 Help on built-in function rename in module nt:

 rename(...)
     rename(old, new)

     Rename a file or directory.

Regards,
Bengt Richter



More information about the Python-list mailing list