Why exception from os.path.exists()?

Marko Rauhamaa marko at pacujo.net
Fri Jun 1 10:18:21 EDT 2018


Chris Angelico <rosuav at gmail.com>:

> Possibly more confusing, though, is this:
>
>>>> os.path.exists(1)
> True
>>>> os.path.exists(2)
> True
>>>> os.path.exists(3)
> False
>
> I think it's testing that the file descriptors exist, because
> os.path.exists is defined in terms of os.stat, which can stat a path
> or an FD. So os.path.exists(fd) is True if that fd is open, and False
> if it isn't. But os.path.exists is not documented as accepting FDs.
> Accident of implementation or undocumented feature? Or maybe
> accidental feature?

What's more:

   >>> os.path.exists(-100)
   False
   >>> os.path.exists(-1000000000000)
   Traceback (most recent call last):
     File "<stdin>", line 1, in <module>
     File "/usr/lib64/python3.6/genericpath.py", line 19, in exists
       os.stat(path)
   OverflowError: fd is less than minimum

One could argue -100 is less than minimum...

The common denominator is that "\0" and -1000000000000 are caught by
Python's standard library while "" and -100 are caught by the OS.


Marko



More information about the Python-list mailing list