Why does pathlib not have is_readable() & things like that?

Steven D'Aprano steve at pearwood.info
Tue Apr 26 11:46:26 EDT 2016


On Tue, 26 Apr 2016 11:30 pm, Adam Funk wrote:

> I recently discovered pathlib in the Python 3 standard library, & find
> it very useful, but I'm a bit surprised that it doesn't offer things
> like is_readable() and is_writable.  Is there a good reason for that?

Maybe nobody thought of it. Why don't you propose a feature request on the
bug tracker?

> I've been improvising with things like this:
> 
> import pathlib, os
> 
> path = pathlib.Path('some/directory')
> writable = os.access(str(path), os.W_OK | os.X_OK)
> 
> Is that the best way to do it?

No. All you have learned is that the directory is writable *now*. In a
millisecond, or a minute, when you actually go to write to it, it may no
longer be writable -- it may not even exist.

There is a whole class of serious security vulnerabilities and bugs caused
by the difference between the time you check something and the time you
actually use it. "Time of check to time of use" bugs can be best avoided by
not checking ahead of time whether the directory is writable, but just
*attempting to write to it*, and catching the error if you can't.




-- 
Steven




More information about the Python-list mailing list