Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import sys
- def readlinkabs(l):
- """
- Return an absolute path for the destination
- of a symlink - by: Greg Smith and Chris Laskey
- http://notemagnet.blogspot.com/2009/09/following-symlinks-in-python.html
- """
- assert (os.path.islink(l))
- p = os.path.normpath(os.readlink(l))
- if os.path.isabs(p):
- if os.path.islink(p):
- # print 'symlink to symlink'
- return readlinkabs(p)
- else:
- return p
- else:
- a = os.path.join(os.path.dirname(l), p)
- if os.path.islink(a):
- # print 'relative symlink: %s' % a
- return readlinkabs(a)
- else:
- return a
- def walk(path):
- for name in os.listdir(path):
- full_path = os.path.abspath(os.path.join(path, name))
- if os.path.islink(full_path):
- print '%s is a symlink; resolving...' % full_path
- try:
- target = readlinkabs(full_path)
- print '%s links to %s' % (full_path, target)
- except RuntimeError:
- print '%s is recursive' % full_path
- elif os.path.isdir(full_path):
- walk(full_path)
- if __name__ == '__main__':
- walk(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment