xlistdir -- an iterator version of os.listdir

Mongryong Mongryong at sympatico.ca
Sun Jan 26 16:35:06 CET 2003


It would also be nice to be able to list files allow with a certain
pattern or suffix.

On Sun, 2003-01-26 at 00:17, Terrel Shumway wrote:
> xlistdir.xlistdir is an iterator version of os.listdir.
> 
> It is particularly useful if you have a directory with, for example, 
> 40,000 files, and you only want to look at the first 10.
> 
>   from xlistdir import xlistdir
>   x = xlistdir("directory-with-LOTS-of-files")
>   
>   for i in range(10):
>       print x[i] # or "x.next()"
> 
> 
> You might also use it to filter the list before allocating it:
> 
>   files_only = [ file for file in xlistdir(bigdir) \
> 	if os.path.isfile(os.path.join(bigdir,file)) ]
> 
>   
> Most of this code was cut and pasted from xreadlinesmodule.c and posixmodule.c
> in Python-2.2.2.tgz
> 
> This initial hack may not even compile on platforms without 
> opendir/readdir/closedir (e.g. mswin32, mswin16, OS/2) because I do 
> not have access to those platforms. 
> 
> It does include a unit test, and it does pass for me on Linux. 8-)
> 
> If you can build extension modules on MS_WIN32, MS_WIN16, or OS/2 (or others),
> please try this out.  If you can make it compile and pass the unit tests, 
> you will get a really cool function to use in your programs and share with 
> the rest of the python community 8-).
> 
> 
> Get the code from http://wxidle.sourceforge.net/projects/xlistdir/
> 
> -- Terrel
> 
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list







More information about the Python-list mailing list