os independent rename

Ian Kelly ian.g.kelly at gmail.com
Sat Sep 17 18:41:18 CEST 2011

On Sat, Sep 17, 2011 at 9:58 AM, Lee Harr <missive at hotmail.com> wrote:
> I just got a bug report the heart of which is the
> difference between unix and windows in using
> os.rename
> (ie, "On Windows, if dst already exists, OSError will be raised")
> Hmm, I thought, maybe I'm supposed to use
> shutil here. That is the "high-level" operations.
> Unfortunately, shutil.move says it depends on
> os.rename
> So, what is the best way to do this that will
> behave the same across operating systems?

shutil.move works for me, at least in Python 2.7.  The docstring
doesn't seem to match the code here:

        os.rename(src, real_dst)
    except OSError:
        if os.path.isdir(src):
            if _destinsrc(src, dst):
                raise Error, "Cannot move a directory '%s' into itself
'%s'." % (src, dst)
            copytree(src, real_dst, symlinks=True)
            copy2(src, real_dst)

More information about the Python-list mailing list