0

For a file-system comparison tool in Python I want to find out what the link text of a symbol link is. I do not want to follow the link, bur rather find the link text and whether it is absolute or relative.

Example: For a link which via ls -l prints

lrwxr-xr-x  1 tutor  staff    16B 25 Aug 17:13 etc -> dir-with-dir/etc

I would like to retrieve the text "dir-with-dir/etc" exactly in this relative form.

How can I access this information in Python?

halloleo
  • 9,216
  • 13
  • 64
  • 122

1 Answers1

1

You can use os.readlink(path) to find the linked file of a symbolic link. From the documentation:

Return a string representing the path to which the symbolic link points.

Also check out this more detailed answer in a related SO question: https://stackoverflow.com/a/42426912/3628578

m_____z
  • 1,521
  • 13
  • 22
  • Getting the *resolved* path is exactly what I want to avoid. But the answer to the duplicate question gives me the right clue to use `os.readlink()`. – halloleo Aug 27 '19 at 14:22
  • Cool - I updated my answer just for completeness sake in case someone else gets to this thread. – m_____z Aug 27 '19 at 14:28
  • 1
    Thanks for the edit - step by step making StackOverflow a better place. :-) (BTW, I just corrected the location of the `readlink` function and its doco link: `readlink` resides in the `os` module not in `os.path`. – halloleo Aug 27 '19 at 23:44