Absolute linking in Python
<p>How can I make my links relative to the home directory (absolute linking)? I have a program that will use a file given anywhere in my user account. Code:</p>

<pre><code>file_name = input("Enter file path")
try:
    file = open("../" + file_name)
    print(file)
except:
    print("Failed to open")
</code></pre>

<p>Currently this assumes my program is in my desktop (which it is). Can I make it so that it will work the same regardless of how many folders it is in?</p>

<p><strong>EDIT</strong>: I want to make it relative to the user's home directory.</p>

<pre><code>import os
</code></pre>

<p>home_dir = os.path.expanduser('~')</p>

<pre><code>file_name = input("Enter file path")
try:
    file = open(os.path.join(home_dir, file_name))
    print(file)
except:
    print("Failed to open")
</code></pre>

<p><code>os.path.expanduser('~')</code> should return the home directory of the user.</p>

<p>Though I can't quite tell if you want an absolute or relative path, and whether its to the home directory or your desktop directory that you want. You may want to reword your question.</p>

python python-3.x absolute-path