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?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
Can I mention testing for hardlinks without python? /bin/test has the FILE1 -ef FILE2 condition that is true when files share an inode.
Therefore, something like
find . -type f -exec test \{} -ef /path/to/file \; -print
works for hard link testing to a specific file.Which brings me to reading
man test
and the mentions of-L
and-h
which both work on one file and return true if that file is a symbolic link, however that doesn't tell you if the target is missing.I did find that
head -0 FILE1
would return an exit code of0
if the file can be opened and a1
if it cannot, which in the case of a symbolic link to a regular file works as a test for whether it's target can be read.I'm not a python guy but it looks like os.readlink()? The logic I would use in perl is to use readlink() to find the target and the use stat() to test to see if the target exists.
Edit: I banged out some perl that demos readlink. I believe perl's stat and readlink and python's os.stat() and os.readlink()are both wrappers for the system calls, so this should translate reasonable well as proof of concept code:
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:
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:
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
os.path
You may try using realpath() to get what the symlink points to, then trying to determine if it's a valid file using is file.
(I'm not able to try that out at the moment, so you'll have to play around with it and see what you get)
I had a similar problem: how to catch broken symlinks, even when they occur in some parent dir? I also wanted to log all of them (in an application dealing with a fairly large number of files), but without too many repeats.
Here is what I came up with, including unit tests.
fileutil.py:
Unit tests:
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
It also says:
So if you are worried about permissions, you should add other clauses.