question about endswith()

Matt Funk mafunk at nmsu.edu
Fri Mar 4 11:29:57 EST 2011


Hi,

thanks guys. This is it. The following code will match both hdf and hdf5
for reasons explained in the email from Ethan.

extensions = 'hdf5' #doesn't work
files =
('MOD03.A2010002.1810.005.2010258062733.hdf','MOD03.A2010002.1950.005.2010258063105.hdf','MOD03.A2010002.1950.005.2010258063105.hdf5')
for filename in files:
    if (any(filename.endswith(x) for x in extensions)):
        print filename

The following code will work:
extensions = ['hdf5'] #works
files =
('MOD03.A2010002.1810.005.2010258062733.hdf','MOD03.A2010002.1950.005.2010258063105.hdf','MOD03.A2010002.1950.005.2010258063105.hdf5')
for filename in files:
    if (any(filename.endswith(x) for x in extensions)):
        print filename


thanks for the help
matt


On 3/4/2011 2:26 AM, Tom Zych wrote:
> Ethan Furman wrote:
>> 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.
> Score:5, Insightful.
>
> "Oh, I'm sorry, this is Python. Slashdot is room 12A, next door."
>




More information about the Python-list mailing list