擅长:python、mysql、java
<p>我认为<a href="http://docs.python.org/library/os.path.html#os.path.expanduser" rel="noreferrer">^{<cd1>}</a>可能会有帮助。</p>
<blockquote>
<p>On Unix and Windows, return the argument with an initial component of <code>~</code> or <code>~user</code> replaced by that user‘s home directory.</p>
<p>On Unix, an initial <code>~</code> is replaced by the environment variable HOME if it is set; otherwise the current user’s home directory is looked up in the password directory through the built-in module <code>pwd</code>. <strong>An initial <code>~user</code> is looked up directly in the password directory</strong>.</p>
<p>On Windows, HOME and USERPROFILE will be used if set, otherwise a <strong>combination of HOMEPATH and HOMEDRIVE</strong> will be used. <strong>An initial <code>~user</code> is handled by stripping the last directory component from the created user path derived above</strong>.</p>
<p>If the expansion fails or if the path does not begin with a tilde, the path is returned unchanged.</p>
</blockquote>
<p>所以你可以:</p>
<pre><code>os.path.expanduser('~user')
</code></pre>