[Python-ideas] os.listdir iteration support

Georg Brandl g.brandl at gmx.net
Fri Nov 23 09:06:43 CET 2007


Greg Ewing schrieb:
> 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?
> 
> Maybe what we really want is the functionality of
> the C opendir and readdir functions exposed in the os
> module. Then we could have an explicit method for
> closing the file handle.

What about an os.iterdir() generator which uses opendir/readdir as proposed?
The generator's close() could also call closedir(), and you could have a
warning in the docs about making sure to have it closed at some point.
One could even use an enclosing with closing(os.iterdir()) as d: block.

Georg

-- 
Thus spake the Lord: Thou shalt indent with four spaces. No more, no less.
Four shall be the number of spaces thou shalt indent, and the number of thy
indenting shall be four. Eight shalt thou not indent, nor either indent thou
two, excepting that thou then proceed to four. Tabs are right out.




More information about the Python-ideas mailing list