How can I check how much of a path exists in python
<p>Let's say on my filesystem the following directory exists:</p>

<pre><code>/foo/bar/
</code></pre>

<p>In my python code I have the following path:</p>

<pre><code>/foo/bar/baz/quix/
</code></pre>

<p>How can I tell that only the <code>/foo/bar/</code> part of the path exists? </p>

<p>I can walk the path recursively and check it step by step, but is there an easier way?</p>

<p>I think a simple <code>while</code> loop with <code>os.path.dirname()</code> will suffice the requirement</p>

<pre><code>path_string = '/home/moin/Desktop/my/dummy/path'
while path_string:
    if not os.path.exists(path_string):
        path_string = os.path.dirname(path_string)
    else:
        break
# path_string = '/home/moin/Desktop'  # which is valid path in my system
</code></pre>

python path directory