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>Well, I think the only way is to work recursively... Though, I would work up the directory tree. The code isn't too hard to implement:</p>

<pre><code>import os

def doesItExist(directory):
    if not os.path.exists(directory):
        doesItExist(os.path.dirname(directory)
    else:
        print "Found: " + directory
        return directory
</code></pre>

python path directory