[Python-Dev] os.path function for “get the real filename”

Greg Ewing greg.ewing at canterbury.ac.nz
Sat Sep 25 03:55:55 CEST 2010


Ben Finney wrote:

> Your heuristics seem to assume there will only ever be a maximum of one
> match, which is false. I present the following example:
> 
>     $ ls foo/
>         bAr.dat  BaR.dat  bar.DAT

There should perhaps be an extra step at the beginning:

0) Test whether the specified path refers to an existing
file. If not, raise an exception.

If that passes, and the file system is case-sensitive, then
there must be a directory entry that is an exact match, so
it will be returned by step 1.

If the file system is case-insensitive, then there can be
at most one entry that matches except for case, and it must
be the one we're looking for, so there is no need for the
extra test in step 2.

So the revised algorithm is:

0) Test whether the specified path refers to an existing
    file. If not, raise an exception.

1) Search the directory for an exact match, return it if found.

2) Search for a match ignoring case, and return one if found.

3) Otherwise, raise an exception.

There's also some prior art that might be worth looking at:
On Windows, Python checks to see whether the file name of an
imported module has the same case as the name being imported,
which is a similar problem in some ways.

> It seems to me this whole thing should be hashed out on ‘python-ideas’.

Good point -- I've redirected the discussion there.

-- 
Greg



More information about the Python-Dev mailing list