[Tutor] glob or filter help

Jay Loden python at jayloden.com
Sat Jan 22 05:17:23 CET 2005


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?

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?

The script is working now, (I know, I know, if it ain't broke, don't fix 
it...) but I want to be a better programmer so more elegant solutions are 
accepted gratefully. 
-Jay


More information about the Tutor mailing list