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>No easy function in the standard lib but not really a difficult one to make yourself.</p>

<p>Here's a function that takes a path and returns only the path that does exist.</p>

<pre><code>In [129]: def exists(path):
     ...:     if os.path.exists(path): return path
     ...:     return exists(os.path.split(path)[0])
     ...:

In [130]: exists("/home/sevanteri/src/mutta/oisko/siellÃ¤/jotain/mitÃ¤/ei ole/")
Out[130]: '/home/sevanteri/src'
</code></pre>

python path directory