question about endswith()

Ethan Furman ethan at stoneleaf.us
Thu Mar 3 19:31:53 EST 2011


Matt Funk wrote:
> Hi,
> i have a list of files, some of which end with .hdf and one of them end
> with hdf5. I want to filter the hdf5 file. Thereforei set extensions: hdf5
> I try to filter as below:
 >
> -->if (any(filename.endswith(x) for x in extensions)):

What is extensions?  A string or a tuple?  I'm guessing a string, 
because then you're looking at:

--> filename.endswith(x) for x in 'hdf5'

which is the same as

--> filename.endswith('h') or filename.endswith('d') or
     filename.endswith('f') or filename.endswith('5')

and then both .hdf and .hdf5 files will get matched.

Try making extensions a tuple.

Hope this helps, but if it doesn't, follow Grant's advice.

~Ethan~



More information about the Python-list mailing list