Looping over a list question

Christoph Haas email at christoph-haas.de
Tue Oct 3 14:20:08 EDT 2006


On Tuesday 03 October 2006 19:50, stephen at theboulets.net wrote:
> I found myself writing:
>
> for f in [i for i in datafiles if '.txt' in i]:
>     print 'Processing datafile %s' % f
>
> but I was wishing that I could have instead written:
>
> for f in in datafiles if '.txt' in f:
>     print 'Processing datafile %s' % f

What about:


import glob
for filename in glob.glob('*.txt'):
    print "Processing file", filename
    ...


 Christoph



More information about the Python-list mailing list