Jay Loden wrote: > I have the following code in my updates script (gets the five most recent > updated files on my site) > > def get_fles(exts, upd_dir): > '''return list of all the files matching any extensions in list exts''' > fle_list = [] > for each in exts: > cmd = upd_dir + "*." + each > ext_ls = glob.glob(cmd) > fle_list = fle_list + ext_ls > return filter(notlink, fle_list) > > I wanted to just get one list, of all the .htm and .exe files in my upd_dir. > I was trying to make a far more elegant solution that what's above, that > could generate a list through a filter. Is there a way to trim the code down > to something that does ONE sort through the directory and picks up the .htm > and .exe files? (note, it is not necessary for this to recurse through > subdirectories in the upd_dir). I have cmd defined above because calling > "glob.glob(upd_dir + "*." + each) returned the error "cannot concatenate > string and list objects" - is this the only way around that, or is there a > better way?
How about: >>> import os >>> exts = ('gz', 'conf') >>> upd_dir = '.' >>> filter(lambda fn : fn.split('.')[-1] in exts, os.listdir(upd_dir)) Do you like this better. It doesn't use glob and is terse. > Also in the above code, "notlink" is just a function that returns True if > "islink()" returns true....there has to be a better way to use this with > filter(), how can i make filter use "if islink()!=true" as its condition? If the filter criteria is complex, it deserves it's own function: >>> import os >>> import os.path >>> def get_files(exts, upd_dir): ... def criteria(filename): ... return filename.split('.')[-1] in exts \ ... and not os.path.islink(filename) ... return filter(criteria, os.listdir(upd_dir)) ... >>> get_files(('gz', 'conf'), '.') ['dynfun.pdf.gz', 'twander-3.160.tar.gz', 'PyCon2004DocTestUnit.pdf.gz', 'arg23.txt.gz', '.fonts.conf'] Javier _______________________________________________ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor