Why exception from os.path.exists()?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Fri Jun 1 19:16:32 EDT 2018


On Fri, 01 Jun 2018 14:38:56 +0000, Grant Edwards wrote:

> On 2018-06-01, Paul Moore <p.f.moore at gmail.com> wrote:
> 
>> Python allows strings with embedded \0 characters, so it's possible to
>> express that question in Python - os.path.exists('a\0b').  What can be
>> expressed in terms of the low-level (C-based) operating system API
>> shouldn't be relevant.
> 
> Python allows floating point numbers, so it is possible to express this
> question in python: os.path.exists(3.14159).  Is the fact that the
> underlying OS/filesystem can't identify files via a floating point
> number relevent?  Should it return False or raise ValueError?

Certainly not a ValueError, that would be silly. The fact that it is an 
illegal value is subordinate to the fact that it is the wrong type. 

It should either return False, or raise TypeError. Of the two, since 
3.14159 cannot represent a file on any known OS, TypeError would be more 
appropriate.

But since "\0" is the correct type (a string), and the fact that it 
happens to be illegal on POSIX is a platform-dependent detail of no more 
importance than the fact that "?" is illegal on Windows, it should be 
treated as any other platform-dependent illegal file and return False.


-- 
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