title: How to get symlink target in Python?
body: <p>Using Python, I need to check whether hundreds of symlinks are correct and recreate them when not. What I do now is to compare real paths of what I want and what I have, but it's slow because it's over NFS with an automount.</p>
  <p>Otherwise I'm going to run a subprocess with the command '<code>ls -l</code>' and work on the list of strings returned. I would prefer a better solution, using a Python library...</p>
  <p><strong>Edit1:</strong> I have: <code>link_name -&gt; link_target</code> and then <code>link_target -&gt; a_real_file</code>. What I need is to extract <code>link_target</code> from <code>link_name</code>, not <code>a_real_file</code>. I don't care if the real file does not exist.</p>
  <p><strong>Edit2:</strong> Maybe I did not express correctly. What I mean by a correct symlink is '<em>a link that point to a predefined path, even if it does not exist</em>'. So I need to check that:</p>
  <pre><code>link_name_1 -&gt; target_1
  link_name_2 -&gt; target_2
  </code></pre>
  <p>That's why I need to extract targets, not the real paths. Then I compare them to a reference (dictionary). So my question is: How do I extract the target path?</p>