Why exception from os.path.exists()?

Marko Rauhamaa marko at pacujo.net
Thu May 31 09:38:27 EDT 2018


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.


Marko



More information about the Python-list mailing list