Why exception from os.path.exists()?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Sun Jun 10 20:28:46 EDT 2018


On Sun, 10 Jun 2018 22:09:39 +0100, Barry Scott wrote:

> Singling out os.path.exists as a special case I do think is reasonable.
> All functions that take paths need to have a consistent response to data

The *mere existence* of os.path.exists means that there is not a 
consistent response to file names:

    open(foo) raises an exception if foo doesn't exist;

    os.path.exists(foo) returns False if foo doesn't exist.

There is no requirement that different functions do the same thing with 
the same bad input. The *whole point* of o.p.exists is to return False, 
not raise an exception.


> that is impossible to pass to the OS.

Even if it were true that file names cannot contain certain characters, 
and it is not, why is that a distinction that anyone gives a shit about?

I do not expect that there are more than a handful of use-cases for 
distinguishing "file names which cannot be passed to the OS" versus "any 
other illegal file name". And even that is generous.

Besides, it is certainly not true that there are no OSes that can deal 
with NULs in file names. Classic Mac OS can, as filenames there are 
represented as Pascal strings (a length byte followed by an array of 
arbitrary bytes), not NUL-terminated C strings.




-- 
Steven D'Aprano
"Ever since I learned about confirmation bias, I've been seeing
it everywhere." -- Jon Ronson




More information about the Python-list mailing list