Why exception from os.path.exists()?

Marko Rauhamaa marko at pacujo.net
Mon Jun 11 04:40:15 EDT 2018


Barry Scott <barry at barrys-emacs.org>:
> I think the rule is, if python can pass the string faithfully to the
> OS, then do so, otherwise raise an exception that tells the programmer
> that they are doing something that the OS does not allow for.

Sure, but few application programmers would think of dealing with the
surprising ValueError. If they did, they wouldn't think os.path.exists()
had any usefulness. Why write:

    try:
        ex = os.path.exists(path)
    except ValueError:
        ex = False
    if not ex:
        ...

instead of:

    try:
        os.stat(path)
    except (OSError, ValueError):
        ...


Marko



More information about the Python-list mailing list