forced overwrite in os.symlink

Fredrik Lundh effbot at
Fri Mar 10 08:50:54 CET 2000

Benyang Tang <btang at> 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


> 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):
        os.symlink(file1, file2)
    except OSError, e:
        if e.errno == errno.EEXIST:
            os.symlink(file1, file2)


<!-- (the eff-bot guide to) the standard python library:

More information about the Python-list mailing list