Find broken symlinks with Python

后端 未结 8 1469
广开言路
广开言路 2020-12-15 05:17

If I call os.stat() on a broken symlink, python throws an OSError exception. This makes it useful for finding them. However, there are

8条回答
  •  感动是毒
    2020-12-15 05:30

    I used this variant, When symlink is broken it will return false for the path.exists and true for path.islink, so combining this two facts we may use the following:

    def kek(argum):
        if path.exists("/root/" + argum) == False and path.islink("/root/" + argum) == True:
            print("The path is a broken link, location: " + os.readlink("/root/" + argum))
        else:
            return "No broken links fond"
    

提交回复
热议问题