Why exception from os.path.exists()?

Marko Rauhamaa marko at pacujo.net
Mon Jun 11 07:23:42 EDT 2018


"Peter J. Holzer" <hjp-python at hjp.at>:

> On 2018-06-11 01:06:37 +0000, Steven D'Aprano wrote:
>> Baking a limitation of some file systems into the high-level
>> interface is simply a *bad idea*.
>
> We aren't talking about a high-level interface here.

Call it high-level or not, we *are* talking about an interface
("os.path") whose whole raison d'être is abstracting OS specifics from
basic pathname processing.

> We are talking about low-level code which is just above the OS. THAT
> code MUST make sure that it calls the OS API with meaningful
> parameters or not at all. And it should raise an Exception in the
> latter case. And that exception should not be misleading.

I respectfully disagree. You are breaking the illusion os.path seeks to
provide. What you are saying, essentially, is that os.path should not
exist.

Since it does exist and can't be wished away *and* since the trap we are
talking about ensnares well-meaning developers, there should be a
*practical* defense against accidents, which could be serious.

>> How would you feel if there were a whole lot of ignorant Pascal
>> programmers arguing that it was fundamentally impossible for file
>> names to exceed 255 characters, and therefore os.path.exists() out to
>> raise ValueError when passed a file name of 256 characters?
>
> You are barking up the wrong tree here.

I believe the "wrong tree" would want a ValueError to be raised in this
situation.

Funny thing is, nothing in the os.path.exists() API suggests what kinds
of illegal value result in a ValueError and what result in a False. The
application developer has no way of guaranteeing a ValueError won't take
place. It should normally be possible for an application to avoid a
ValueError (or TypeError) if it so chose.


Marko



More information about the Python-list mailing list