[Tutor] Renaming directories

Magnus Lyckå magnus@thinkware.se
Wed May 7 09:05:01 2003


At 13:15 2003-05-07 +0200, Charlie Clark wrote:
 >From the module reference:

>"""Rename the file or directory src to dst. If dst is a directory, OSError
>will be raised.

Read carefully: "If dst *is* a directory..." (my emphasis)

I.e., if you already have a directory 'a', *and* a directory 'b',
os.rename('a', 'b') will cause an OSError. It won't just swipe
an existing directory away. If you really want to do that, you
have to remove or rename the destination directory first.

As furhter noted, you need to do a corresponding operation on
simple files as well if you run on Windows.

>As I am windows I do get OSError on all directories which I need to rename
>as some of the programs (like Python) are case sensitive even if Windows
>itself isn't.

I don't know what case sensitivity has to do with this.
NTFS is certainly case sensitive, even if it doesn't
allow the coexistence of files or directories whose names
only differ in case.

Please tell us what you are *really* trying to do. It's
helpful if you show us your code and your failed session,
including *exact* error message. See mine below:

As you see, I have no problems with this:

H:\tmp>ver

Microsoft Windows 2000 [Version 5.00.2195]

H:\tmp>md test1

H:\tmp>dir test?
  Volymen i enhet H har etiketten mly
  Volymens serienummer är 104A-0151

  Innehåll i katalogen H:\tmp

2003-05-07  15:29       <KAT>          test1
                0 fil(er)                   0 byte
                1 katalog(er)     310 804 480 byte ledigt

H:\tmp>python -c "import os; os.rename('test1', 'test2')"

H:\tmp>dir test?
  Volymen i enhet H har etiketten mly
  Volymens serienummer är 104A-0151

  Innehåll i katalogen H:\tmp

2003-05-07  15:29       <KAT>          test2
                0 fil(er)                   0 byte
                1 katalog(er)     310 804 480 byte ledigt

H:\tmp>rd test2

But I can't do this (just as expected). BTW, touch is a unix command,
available on Windows through cygwin. It just creates an empty file in
this case.

H:\tmp>md test1

H:\tmp>md TEST2

H:\tmp>touch test1/x1.txt

H:\tmp>touch TEST2/x2.txt

H:\tmp>python -c "import os; os.rename('test1', 'test2')"
Traceback (most recent call last):
   File "<string>", line 1, in ?
OSError: [Errno 13] Permission denied

H:\tmp>python -c "import os; os.rename('test1', 'test3')"

On Windows I can't rename test1 to test2 if there is already
a TEST2, since these names are considered to be equivalent.

H:\tmp>python -c "import os; os.rename('test3', 'test1')"

H:\tmp>python -c "import os; os.rename('Test1', 'test3')"

I'd say this behaves just as one would expect on Windows.
I can give source directory name as "Test1", even if it's
really "test1", since these are equivalent.


--
Magnus Lycka (It's really Lyck&aring;), magnus@thinkware.se
Thinkware AB, Sweden, www.thinkware.se
I code Python ~ The shortest path from thought to working program