Detecting case mismatch on filename in Windows (preferably using python)?
case-sensitive, python, windows
Solution
os.listdir 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.
So you need to do this check at each level of the path:
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))
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):
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))
You may need to adapt this if , e.g., `foo/bar` is not to be taken to mean that `foo` 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;-).
Implementation notes: I'm taking advantage of the fact that `zip` 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 `levels` in the first argument, `zip` does it for me. `all` will short circuit where it can, returning `False` as soon as it detects a false value, so it's just as good as an explicit loop but faster and more concise.
Problem
I have some xml-configuration files that we create in a Windows environment but is deployed on Linux. These configuration files reference each other with filepaths. We've had problems with case-sensitivity and trailing spaces before, and I'd like to write a script that checks for these problems. We have Cygwin if that helps. Example: Let's say I have a reference to the file foo/bar/baz.xml, I'd do this ``` <someTag fileref="foo/bar/baz.xml" /> ``` Now if we by mistake do this: ``` <someTag fileref="fOo/baR/baz.Xml " /> ``` It will still work on Windows, but it will fail on Linux. What I want to do is detect these cases where the file reference in these files don't match the real file with respect to case sensitivity.