[Tutor] Renaming directories

Charlie Clark charlie@begeistert.org
Wed May 7 12:03:01 2003


On 2003-05-07 at 15:05:58 [+0200], you wrote:
> 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.
> 
> 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.

Oh I'll admit the horrible truth: Windows overwrote part of my BeOS 
partition when coming out of hibernation (didn't mention it was doing it). 
Of course, according to Microsoft I'm probably breaking the licence 
agreement by installing another operating system on _my_ machine anyway so 
their software is perfectly within its rights to correct matter! While 
trying to restore a single file from the BeOS with "dd" I made a mistake 
and BeOS got it's revenge and overwrote some of the windows partition :-( 
I've found a program which seems to do a reasonable job of restoring 
things. It seems to have had a problem with filenames: all lowercase have 
been restored as all uppercase and this affects Python modules in my Zope 
distro :-(

It's a FAT partition so I didn't think this would matter but it does.
 
> 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:

import os

file_list = []

def clean(file_list, dir, files):
    for file in files:
        if file.upper() == file:
            fp = os.path.join(dir, file)
            if os.path.isfile(fp):
                os.rename(fp, fp.lower())
            else:
                file_list.append(fp)
##            print "%s needs renaming" %file
##            os.rename(file, file.lower())
            file_list.append(file)

os.path.walk(".", clean, file_list)

out = open("dump.txt", "w")
out.write("\n".join(file_list))
##print file_list 

and these are some of the things which don't get renamed:

.\usr\LOCAL\BIN
BIN
.\usr\LOCAL\DOC
DOC
.\usr\LOCAL\INCLUDE
INCLUDE
.\usr\LOCAL\KANNEL
KANNEL

So it seems it's not possible to rename folders in place in windows using 
Python. What are my options? What I think I can do is create a new "root" 
folder and store everything in that using os.renames() which will create 
the folders required. Sound reasonable?

Charlie