xlistdir -- an iterator version of os.listdir

Terrel Shumway tshumway at jdiworks.net
Sun Jan 26 00:17:23 EST 2003


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






More information about the Python-list mailing list