[Tutor] Renaming directories

Charlie Clark charlie@begeistert.org
Wed May 7 07:16:02 2003


On 2003-05-07 at 13:11:08 [+0200], you wrote:
> There is no such thing as os.path.rename that I know of. At least not in 
> Python 2.2.
> 
> os.rename should work though.

duh, sorry. Feels like a "path" function.

>  >>> import os
>  >>> help(os.rename)
> Help on built-in function rename:
> rename(...)
>      rename(old, new) -> None
>      Rename a file or directory.
> 
> It works for me. I don't think we can help you unless you provide more 
> details, such as what OS, what kind of file system and in particular: 
> What is the error message?
> 
> I can imagine that the directory is locked for some reason.

>From the module reference:

"""Rename the file or directory src to dst. If dst is a directory, OSError 
will be raised. On Unix, if dst exists and is a file, it will be removed 
silently if the user has permission. The operation may fail on some Unix 
flavors if src and dst are on different filesystems. If successful, the 
renaming will be an atomic operation (this is a POSIX requirement). On 
Windows, if dst already exists, OSError will be raised even if it is a 
file; there may be no way to implement an atomic rename when dst names an 
existing file. Availability: Macintosh, Unix, 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.

Charlie