I found a clue as to the way to handle both cases in the PathModule code, which is that you use os.path.isabs on the result to figure out what you got back. That module is a lot of baggage to pull in if you just want to correct this one issue, though, so here's a simpler function that knows how to handle both cases:
def readlinkabs(l):
"""
Return an absolute path for the destination
of a symlink
"""
assert (os.path.islink(l))
p = os.readlink(l)
if os.path.isabs(p):
return p
return os.path.join(os.path.dirname(l), p)
I hope my search engine cred bubbles me up so someone else trying to look this up like I did doesn't have to bother reinventing this particular tiny wheel.
2 comments:
Thanks for the tip. I was wondering about this myself.
Chris Laskey writes:
The unpredictable behavior of os.readlink() means your function needs a simple addition to completely tame it.
The problem is when os.readlink() returns a relative path, it may or may not prepend "./" to the returned path. If it is prepended, the final os.path.join() will return an incorrect absolute path.
A simple solution is to simply wrap the p=os.readlink(l) line with a call to os.path.normpath(), making the new line:
p = os.path.normpath(os.readlink(l))
Post a Comment