Why exception from os.path.exists()?

Marko Rauhamaa marko at pacujo.net
Fri Jun 8 03:45:32 EDT 2018


Antoon Pardon <antoon.pardon at vub.be>:

> On 08-06-18 04:19, Steven D'Aprano wrote:
>> On Thu, 07 Jun 2018 17:45:06 +1000, Chris Angelico wrote:
>>
>>> So... an ASCIIZ string *can* contain that character, or
>>> at least a representation of it. Yet it cannot contain "\0".
>> [...]
>> NTFS file systems use UTF-16 encoded strings. For typical mostly-ASCII 
>> pathnames, the bytes on disk are *full* of NUL bytes. 
>
> This is irrelevant.

As for what is relevant for the original question is that the ValueError
exception is a practical trap that I have fallen into, and, as I
demonstrated yesterday, the http.server module has fallen into (through
os.path.isdir()). In fact, I couldn't spot a single instance of
os.path.exists() in the Python standard library that would guard against
a ValueError (to be sure, in almost all of the cases you could argue it
would be redundant).

Whatever your philosophical tastes, this unexpected feature of
os.path.exists() (& co) leads to unexpected application behavior IRL,
and, in the GhostBusters sense, that is bad.


Marko



More information about the Python-list mailing list