Problem with case insensitive volumes
Gabriel Genellina
gagsl-py2 at yahoo.com.ar
Tue May 5 22:24:32 EDT 2009
En Tue, 05 May 2009 12:55:13 -0300, spillz <damienlmoore at gmail.com>
escribió:
>
> /my/path/to/usbkey/and/file
>
> everything up to "/my/path/to/usbkey" is case sensitive. only the "and/
> file" is case insensitive. but my list of stored paths might include
> all images under "/my". thus
>
> /my/path/to/usbkey/and/file1==/my/path/to/usbkey/and/FILE1
> /my/path/to/usbkey/and/file1==/my/path/to/usbkey/AND/FILE1
> but
> /my/path/to/usbkey/and/file1!=/my/path/TO/usbkey/AND/FILE1
>
> so to do this right I'd need to establish which parts of the path are
> case insensitive. being able to retrieve the actual path from an
> equivalent representation would be so much simpler.
I use this function on Windows to obtain the true case name of a file:
<code>
from win32api import FindFiles
from win32file import FILE_ATTRIBUTE_DIRECTORY
from os.path import normpath, split, join
def truecase(fullpath, _cache={}):
"""
Normalize fullpath, returning names using the
same case as actually used in the filesystem.
"""
fullpath = normpath(fullpath)
dirname, filename = split(fullpath)
dirname = dirname.lower()
if not filename:
# drive letter
result = _cache[dirname] = dirname.upper()
return result
if dirname in _cache: dirname = _cache[dirname]
elif dirname: dirname = truecase(dirname)
finddata = FindFiles(fullpath)
if not finddata:
import errno, os
raise IOError(errno.ENOENT, os.strerror(errno.ENOENT), fullpath)
if len(finddata)!=1:
raise ValueError("Too many results for %r" % fullpath)
attr = finddata[0][0]
filename = finddata[0][8]
result = join(dirname, filename)
if attr & FILE_ATTRIBUTE_DIRECTORY:
_cache[result.lower()] = result
return result
</code>
truecase("c:")=="C:"
truecase("C:\\")=="C:\\"
truecase("c:\\")=="C:\\"
truecase("C:\\TeMp")=="C:\\TEMP"
truecase("C:\\TeMp\\")=="C:\\TEMP\\"
truecase(r"C:\Documents and Settings\gabriel")==r"C:\Documents and
Settings\gabriel"
truecase(r"c:\dOcUmEnTs aNd SeTtInGs\GaBrIeL\mIS DOCUMENTOS\105_3 - nist -
sPECIFICATIONS vOLUMETRIC fIELD sTANDARD.PDF")==r"C:\Documents and
Settings\gabriel\Mis documentos\105_3 - NIST - Specifications Volumetric
Field Standard.pdf"
truecase(r"E:\prog\test\compface\Debug\No existe.txt")
--
Gabriel Genellina
More information about the Python-list
mailing list