[Python-ideas] os.listdir iteration support

Greg Ewing greg.ewing at canterbury.ac.nz
Fri Nov 23 12:11:35 CET 2007


Georg Brandl wrote:
> What about an os.iterdir() generator which uses opendir/readdir as proposed?

I was feeling in the mood for a diversion, so I whipped up
a Pyrex prototype of an opendir() object that can be used
either as a file-like object or an iterator.

Here's the docstring:

   """opendir(pathname) --> an open directory object

   Opens a directory and provides incremental access to
   the filenames it contains. May be used as a file-like
   object or as an iterator.

   When used as a file-like object, each call to read()
   returns one filename, or an empty string when the end
   of the directory is reached. The close() method should
   be called when finished with the directory.

   The close() method should also be called when used as
   an iterator and iteration is stopped prematurely. If
   iteration proceeds to completion, the directory is
   closed automatically."""

Source, setup.py and a brief test attached.

--
Greg
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: opendir.pyx
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20071124/8a5a0a90/attachment.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: setup.py
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20071124/8a5a0a90/attachment-0001.ksh>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: test.py
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20071124/8a5a0a90/attachment-0002.ksh>


More information about the Python-ideas mailing list