[Python-Dev] Alternative path suggestion

Giovanni Bajo rasky at develer.com
Sat May 6 14:37:22 CEST 2006


Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:

>> So I suggest splitting the internal data into 'path elements
>> separated by os.sep', 'name elements separated by os.extsep'
>
> What bothers me about that is that in many systems
> there isn't any formal notion of an "extension",
> just a convention used by some applications.
>
> Just because I have a "." in my filename doesn't
> necessarily mean I intend what follows to be
> treated as an extension.

This is up to the application to find out or deduce. Python still needs a
proper support for this highly diffused conventions. I would say that *most* of
the files in my own hard-disk follow this convention (let's see: *.html, *.py,
*.sh, *.cpp, *.c, *.h, *.conf, *.jpg, *.mp3, *.png, *.txt, *.zip, *.tar, *.gz,
*.pl, *.odt, *.mid, *.so... ehi, I can't find a file which it doesn't follow
this convention). Even if you can have a Python file without extension, it
doesn't mean that an application should manually extract "foo" out of "foo.py"
just because you could also name it only "foo".

>> assert pth.basepath == HOMEDIR
>> assert pth.dirparts == ('foo', 'bar')
>> assert pth.nameparts == ('baz', 'tar', 'gz')
>
> What if one of the dirparts contains a character
> happening to match os.extsep? When you do
> pth2 = pth[:-1], does it suddenly get split up
> into multiple nameparts, even though it's actually
> naming a directory rather than a file?
>
> (This is not hypothetical -- it's a common convention
> in some unix systems to use names like "spam.d" for
> directories of configuration files.)

Yes. And there is absolutely nothing wrong with it. Do you have in mind any
real-world case in which an algorithm is broken by the fact that splitext()
doesn't magically guess what's an extension and what is not? People coding
Python applications *know* that splitext() will split the string using the
rightmost dot (if any). They *know* it doesn't do anything more than that; and
that it does that on any path string, even those where the rightmost dot
doesn't indicate a real extension. They know it's not magical, in that it can't
try to guess whether it is a real extension or not, and so does the simple,
clear, mechanical thing: it splits on the right-most dot.

And even if they know this "limitation" (if you want to call it so, I call it
"clear, consistent behaviour which applies to a not-always-consistently-used
convention"), the function is still useful.

Giovanni Bajo



More information about the Python-Dev mailing list