forced overwrite in os.symlink

Fredrik Lundh effbot at telia.com
Fri Mar 10 02:50:54 EST 2000


Benyang Tang <btang at pacific.jpl.nasa.gov> wrote:
> In using
>
> os.symlink(file1,file2)
>
> if file2 already exists, it results in an os.error. Is there a
> function for overwriting an existing file

nope.

> or do I have to use
>
> os.system('ln -sf ' + file1 + ' ' + file2)

not really.  how about this:

import os, errno

def force_symlink(file1, file2):
    try:
        os.symlink(file1, file2)
    except OSError, e:
        if e.errno == errno.EEXIST:
            os.remove(file2)
            os.symlink(file1, file2)

</F>

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->





More information about the Python-list mailing list