[Python-ideas] PEP: Extended stat_result (First Draft)
Andrew Barnert
abarnert at yahoo.com
Mon May 6 23:42:52 CEST 2013
One thing nobody's mentioned yet is that os.path.isdir(f) or os.path.islink(f) isn't just a performance issue, it's a race condition. In face, most code that uses os.path.isfoo is just wrong for similar reasons. Usually you just want EAFTP--and, when you don't, usually you want to open and then fstat. But even when you do want stat, you dont want to call it multiple times.
Discouraging naive users from writing this kind of code is the whole reason the proposal is a good idea.
So, maybe the right answer isn't adding more methods to os.path or renaming the existing ones, but deprecating them entirely.
And with that in mind:
On May 6, 2013, at 12:46, Pieter Nagel <pieter at nagel.co.za> wrote:
> On Mon, 2013-05-06 at 15:32 -0400, random832 at fastmail.us wrote:
>
>> Also,
>> they silently fail (i.e. return False) rather than let exceptions be
>> raised from stat
>
> I think this is actually good design. It makes total sense to me that
> os.path.isfile('/nonexistent') return False. Of course it isn't a file,
> since it doesn't exist. It's not a directory, symlink.. either, for the
> same reason.
>
> It would be most inconvenient if one needed to guard os.path.isfile()
> with a os.path.exists() first.
I was against the return-false part of the proposal, but now I'm not sure.
You almost always want to guard with try, not os.path.exists. The fact that even people who obviously know better still get this wrong implies that making novices guard it manually may be dangerous.
More information about the Python-ideas
mailing list