[Python-ideas] os.listdir iteration support

Aahz aahz at pythoncraft.com
Fri Nov 23 15:39:39 CET 2007

On Fri, Nov 23, 2007, Adam Atlas wrote:
> On 22 Nov 2007, at 23:59, Aahz wrote:
>> The problem is that reading a directory requires an open file handle;
>> given a generator context, there's no clear mechanism for determining
>> when to close the handle.
> Whenever the generator is __del__ed, or whenever the iteration  
> completes, whichever comes first?

Enh.  That is not reliable without work, and getting it reliable is a
waste of work.  The proposed idea for adding an opendir() function is
workable, but it still doesn't solve the need for closing the handle
within listdir().

No matter what, changes the semantics of listdir() to leave a handle
lying around is going to cause problems for some people.

>> Because the list needs to be created in the first place
> How so?

If you're going to ask a question, it would be nice to leave the entire
original context in place, especially given that it's not a particularly
long chunk of text.

Anyway, the Windows case aside, if you don't have a reliable close()
mechanism, you need to slurp the whole thing into a list in one swell
foop so that you can just close the handle.  Even in the Windows case,
you need a handle, and I don't know what the consequences are of leaving
it lying around.
Aahz (aahz at pythoncraft.com)           <*>         http://www.pythoncraft.com/

"Typing is cheap.  Thinking is expensive."  --Roy Smith

More information about the Python-ideas mailing list