Why exception from os.path.exists()?

Serhiy Storchaka storchaka at gmail.com
Sat Jun 2 01:04:19 EDT 2018


02.06.18 03:05, Chris Angelico пише:
> The permissions error is reported differently by stat, but then
> exists() just says "oh that's an OS error so we're going to say it
> doesn't exist". If you truly want the reliable form of os.path.exists,
> it would be this:
> 
> try:
>      os.stat(path)
>      return True
> except FileNotFoundError:
>      return False
> 
> Anything that ISN'T "this file exists" or "this file doesn't exist"
> will be signalled with an exception.

And this is how pathlib.Path.exists() was implemented. But for 
os.path.exists() we are limited by backward compatibility.




More information about the Python-list mailing list