how to generalize/get base directory for Linux path for python script
<p>(Linux)in Bash I can use variableName= cd $BASEDIR./Desktop to replace HOME/userName/Destop.  How can I achieve the same in Python?</p>

<p>EXAMPLE:
        need to replace home/name to be generic so that I can run this from multiple hosts.
    f = open("home/name/directory/subdirectory/file.cfg", "r")</p>

<p>You might get some use out of <a href="https://docs.python.org/2/library/os.path.html#os.path.expanduser" rel="nofollow"><code>os.path.expanduser</code></a> or <a href="https://docs.python.org/2/library/os.path.html#os.path.expandvars" rel="nofollow"><code>os.path.expandvars</code></a>:</p>

<pre><code>&gt;&gt;&gt; import os
&gt;&gt;&gt; os.path.expanduser('~/Desktop')
'/home/userName/Desktop'
</code></pre>

<p>or, assuming that <code>$BASEDIR</code> is defined in your environment,</p>

<pre><code>&gt;&gt;&gt; import os
&gt;&gt;&gt; os.path.expandvars('$BASEDIR/Desktop')
'/home/userName/Desktop'
</code></pre>

<p>Using the first option, maybe you can do what you want to do with:</p>

<pre><code>f = open(os.path.expanduser("~/directory/subdirectory/file.cfg"), "r")
</code></pre>

python