Find broken symlinks with Python

2020-02-09 00:59发布

If I call os.stat() on a broken symlink, python throws an OSError exception. This makes it useful for finding them. However, there are a few other reasons that os.stat() might throw a similar exception. Is there a more precise way of detecting broken symlinks with Python under Linux?

7条回答
放荡不羁爱自由
2楼-- · 2020-02-09 01:37

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

查看更多
登录 后发表回答