[Tutor] File browse/ read filter

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Fri, 23 Feb 2001 01:25:44 -0800 (PST)


On Fri, 23 Feb 2001, Sharriff Aina wrote:

> How does one limit a directory read ( os.listdir() )  to certain types of
> files? I=B4d like to have only Image files displayed.

There are a few approaches you can take.  One is to using the
'glob.glob()' function, which lets you get a directory list that searches
based on a wildcard.  For example:

###
from glob import glob
print glob('./*.jpg')
###

will print us a list of all jpeg files in the current directory.  The
documentation talks about glob.glob (that's fun to say!  *grin*) here:

    http://python.org/doc/current/lib/module-glob.html


glob.glob() itself internally uses os.listdir(), filtering the results out
behind the scenes.  You might want to do the filtering yourself, but
glob.glob() should work pretty well for you.


Good luck!