Partial directory search question

alex23 wuwei23 at gmail.com
Thu Oct 1 02:55:25 CEST 2009


Tim Chase <python.l... at tim.thechases.com> wrote:
> If you're doing more processing than just printing it, your
> for-loop is a better (clearer) way to go.  If you have lots of
> processing code, it might help to do the inverse:
>
>    for filename in os.listdir(location):
>      if not filename.startswith('_'): continue
>      lots()
>      of_processing()
>      and_your_complex_logic()
>      goes()
>      here()

Personally, I'd still go with a generator to drive the for-loop:

    underscored_files = (f for f in os.listdir(<location>) if not
f.startswith('_'))
    for filename in underscored_files:
         etc...

What I'm traversing in the for-loop is far more obvious (to me) from
the name of the generator than from having to parse the first few
lines of the loop. It's a lot easier to genericise that behaviour too.



More information about the Python-list mailing list