擅长:python、mysql、java
<p>这样做很好,但是如果只想获取当前目录的父目录,可以使用<code>os.pardir</code>。你知道吗</p>
<p>例如,在我的机器(Mac OS)上(注意,我当前的工作目录是<code>/User/headless/Pictures/wallpapers</code>):</p>
<pre><code>>>> import os
>>> current_path = os.getcwd()
>>> os.path.abspath(os.path.join(current_path, os.pardir))
'/Users/headless/Pictures'
>>> os.path.abspath(os.path.join(os.path.abspath(os.path.join(current_path, os.pardir)), os.pardir))
'/Users/headless'
</code></pre>
<p>基本上,我所做的只是请求父目录。然后询问父目录的父目录。你知道吗</p>