擅长:python、mysql、java
<p>使用<a href="http://docs.python.org/library/os.path.html#os.path.join">os.path.join</a>将指向<code>Documents</code>目录的路径与<code>completeName</code>(文件名?)由用户提供。</p>
<pre><code>import os
with open(os.path.join('/path/to/Documents',completeName), "w") as file1:
toFile = raw_input("Write what you want into the field")
file1.write(toFile)
</code></pre>
<p>如果希望<code>Documents</code>目录与用户的主目录相对,可以使用以下内容:</p>
<pre><code>os.path.join(os.path.expanduser('~'),'Documents',completeName)
</code></pre>
<hr/>
<p>其他人建议使用<code>os.path.abspath</code>。请注意,<code>os.path.abspath</code>不会将<code>'~'</code>解析到用户的主目录:</p>
<pre><code>In [10]: cd /tmp
/tmp
In [11]: os.path.abspath("~")
Out[11]: '/tmp/~'
</code></pre>