[Python-ideas] Speed up os.walk() 5x to 9x by using file attributes from FindFirst/NextFile() and readdir()

Ben Hoyt benhoyt at gmail.com
Thu Nov 15 01:12:48 CET 2012

> I know that two functions may be better than a keyword, but a
> combinatorial explosion of functions ... isn't.  Even given that
> listdir can't change for backwards compatibility, and given that
> iteration might be better for large directories, I'm still not sure an
> exact analogue is worth it.

"Combinatorial explosion of functions"? Slight exaggeration. :-) I'm
proposing adding two new functions, iterdir and iterdir_stat. And I
definitely think two new functions with pretty standard names and
almost self-documenting signatures is much simpler than keyword args
and magical return values. For example, Python 2.x has dict.items()
and dict.iteritems(), and it's clear what the "iter" version does at a

> Could this be someone combined with your 3rd proposal?  For example,
> instead of returning a list of str (or bytes) names, could you return
> a generator that would yield some sort of File objects?

I considered this, as well as a str subclass with a ".stat" attribute.
But iterdir_stat's (filename, stat) tuple is much more explicit,
whereas the str subclass just seemed too magical -- though I might be
able to be convinced otherwise.

Yes, as you mention, I really think the File/Path object is a rabbit
hole, and the intent of my proposal is very minimal changes / minimal
learning curve -- an incremental addition.


More information about the Python-ideas mailing list