Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><a href="http://docs.python.org/library/os.html#os.listdir" rel="nofollow noreferrer">os.listdir</a> on a directory, in all case-preserving filesystems (including those on Windows), returns the actual case for the filenames in the directory you're listing.</p> <p>So you need to do this check at each level of the path:</p> <pre><code>def onelevelok(parent, thislevel): for fn in os.listdir(parent): if fn.lower() == thislevel.lower(): return fn == thislevel raise ValueError('No %r in dir %r!' % ( thislevel, parent)) </code></pre> <p>where I'm assuming that the complete absence of any case variation of a name is a different kind of error, and using an exception for that; and, for the whole path (assuming no drive letters or UNC that wouldn't translate to Windows anyway):</p> <pre><code>def allpathok(path): levels = os.path.split(path) if os.path.isabs(path): top = ['/'] else: top = ['.'] return all(onelevelok(p, t) for p, t in zip(top+levels, levels)) </code></pre> <p>You may need to adapt this if , e.g., <code>foo/bar</code> is not to be taken to mean that <code>foo</code> is in the current directory, but somewhere else; or, of course, if UNC or drive letters are in fact needed (but as I mentioned translating them to Linux is not trivial anyway;-).</p> <p>Implementation notes: I'm taking advantage of the fact that <code>zip</code> just drop "extra entries" beyond the length of the shortest of the sequences it's zipping; so I don't need to explicitly slice off the "leaf" (last entry) from <code>levels</code> in the first argument, <code>zip</code> does it for me. <code>all</code> will short circuit where it can, returning <code>False</code> as soon as it detects a false value, so it's just as good as an explicit loop but faster and more concise.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload