Why exception from os.path.exists()?

Chris Angelico rosuav at gmail.com
Thu May 31 13:25:57 EDT 2018


On Fri, Jun 1, 2018 at 12:51 AM, MRAB <python at mrabarnett.plus.com> wrote:
> On 2018-05-31 14:38, Marko Rauhamaa wrote:
>>
>> Chris Angelico <rosuav at gmail.com>:
>>>
>>> Do you have an actual use-case where it is correct for an invalid path
>>> to be treated as not existing?
>>
>>
>> Note that os.path.exists() returns False for other types of errors
>> including:
>>
>>   * File might exist but you have no access rights
>>
>>   * The pathname is too long for the file system
>>
>>   * The pathname is a broken symbolic link
>>
>>   * The pathname is a circular symbolic link
>>
>>   * The hard disk ball bearings are chipped
>>
>> I'm not aware of any other kind of a string argument that would trigger
>> an exception except the presence of a NUL byte.
>>
>> The reason for the different treatment is that the former errors are
>> caught by the kernel and converted to False by os.path.exists(). The NUL
>> byte check is carried out by Python's standard library.
>>
> On Windows, the path '<' is invalid, but os.path.exists('<') returns False,
> not an error.
>
> The path '' is also invalid, but os.path.exists('') returns False, not an
> error.
>
> I don't see why '\0' should behave any differently.

Okay, if it's just returning False for all the Windows invalid paths,
then sure, the Unix invalid paths can behave the same way.

Thanks for checking that (you and Paul equally).

ChrisA



More information about the Python-list mailing list