Why exception from os.path.exists()?

Serhiy Storchaka storchaka at gmail.com
Sat Jun 2 00:54:21 EDT 2018


01.06.18 16:58, Chris Angelico пише:
> 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?

Accident of implementation. In Python 3.3 os.stat() became accepting 
integer file descriptors (as os.fstat()). os.path.exists() just passes 
its argument to os.stat().




More information about the Python-list mailing list