question about endswith()

Matt Funk mafunk at nmsu.edu
Thu Mar 3 19:18:12 EST 2011


Hi Grant,
first of all sorry for the many typos in my previous email.

To clarify, I have a python list full of file names called 'files'.
Every single filename has extension='.hdf' except for one file which has
an '.hdf5' extension. When i do (and yes, this is pasted):
        for filename in files:
            if (any(filename.endswith(x) for x in extensions)):
                print filename

However, it will print all the files in list 'files' (that is all files
with file extension '.hdf'). My question is why it doesn't just print
the filename with extensions '.hdf5'?

thanks
matt

On 3/3/2011 4:50 PM, Grant Edwards wrote:
> On 2011-03-03, Matt Funk <mafunk at nmsu.edu> wrote:
>
>> 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)):
>>
>> The problem is that i let's all files though rather than just the hdf5
>> file. Is there anything i am doing wrong?
> Yes, you are doing something wrong.
>
> But, in order for somebody to tell you what you're doing wrong, you'll
> have to post some actual, runnable code and tell us 1) what you
> expect it to do, 2) what you see it do.
>
> IMPORTANT: Do _not_ retype code, input or output into your posting. 
>            Cut/paste both code and input/output into your posting.
>




More information about the Python-list mailing list