List comprehensions and glob

Duncan Booth me at privacy.net
Thu Jul 15 10:31:40 CEST 2004


Robin Becker <robin at reportlab.com> wrote in
news:40F63568.5090303 at jessikat.fsnet.co.uk: 

> I want to use a list of glob patterns to create a single flat list of
> files 
> 
> eg
> 
> P = ['*.txt','*.py']
> 
> I expected I would somehow be able to use list comprehensions to do 
> this, but in the end I could only do this hackish thing
> 
> import operator, glob
> F = reduce(operator.add,[glob.glob(p) for p in P],[])
> 
> 
> is there a more pythonic approach?

If you want a list comprehension then how about:

filenames = [ name for pat in PATTERNS for name in glob.glob(pat) ]



More information about the Python-list mailing list