Find broken symlinks with Python

This is not atomic but it works.

os.path.islink(filename) and not os.path.exists(filename)

Indeed by RTFM (reading the fantastic manual) we see

os.path.exists(path)

Return True if path refers to an existing path. Returns False for broken symbolic links.

It also says:

On some platforms, this function may return False if permission is not granted to execute os.stat() on the requested file, even if the path physically exists.

So if you are worried about permissions, you should add other clauses.


A common Python saying is that it's easier to ask forgiveness than permission. While I'm not a fan of this statement in real life, it does apply in a lot of cases. Usually you want to avoid code that chains two system calls on the same file, because you never know what will happen to the file in between your two calls in your code.

A typical mistake is to write something like:

if os.path.exists(path):
    os.unlink(path)

The second call (os.unlink) may fail if something else deleted it after your if test, raise an Exception, and stop the rest of your function from executing. (You might think this doesn't happen in real life, but we just fished another bug like that out of our codebase last week - and it was the kind of bug that left a few programmers scratching their head and claiming 'Heisenbug' for the last few months)

So, in your particular case, I would probably do:

try:
    os.stat(path)
except OSError, e:
    if e.errno == errno.ENOENT:
        print 'path %s does not exist or is a broken symlink' % path
    else:
        raise e

The annoyance here is that stat returns the same error code for a symlink that just isn't there and a broken symlink.

So, I guess you have no choice than to break the atomicity, and do something like

if not os.path.exists(os.readlink(path)):
    print 'path %s is a broken symlink' % path

os.lstat() may be helpful. If lstat() succeeds and stat() fails, then it's probably a broken link.