filter in for loop

Timo timo.linna at gmail.com
Thu Aug 28 07:12:42 EDT 2008


On 28 elo, 13:20, GHZ <geraint.willi... at gmail.com> wrote:

>
> is there a shortcut I'm missing?

(Adding to Bruno's comment)

This kind of style provides a reusable filter/generator-function and
as a bonus, the for-loop becomes easier to understand.

from os.path import abspath
from glob import iglob

def ipathnames(pattern):
    return (abspath(n) for n in iglob(pattern))

for xmlfile in ipathnames('*.xml'):
    <do something>

-- timo



More information about the Python-list mailing list