I think you want do this:<br><br>>>> files = ['file1.hdf', 'file2.hdf', 'file3.hdf5','file4.hdf']<br>>>> print(''.join(x for x in files if x.endswith('5')))<br>
file3.hdf5<br>>>><br><br>But if you need to use it later:<br><br>>>> file_hdf5 = [x for x in files if x.endswith('5')]<br>>>> file_hdf5<br>['file3.hdf5']<br>>>> <br><br>
<br><div class="gmail_quote">2011/3/4 Grant Edwards <span dir="ltr"><invalid@invalid.invalid></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
<div class="im">On 2011-03-04, Matt Funk <<a href="mailto:mafunk@nmsu.edu">mafunk@nmsu.edu</a>> wrote:<br>
> Hi Grant,<br>
> first of all sorry for the many typos in my previous email.<br>
><br>
> To clarify, I have a python list full of file names called 'files'.<br>
> Every single filename has extension='.hdf' except for one file which has<br>
> an '.hdf5' extension. When i do (and yes, this is pasted):<br>
>         for filename in files:<br>
</div><div class="im">>             if (any(filename.endswith(x) for x in extensions)):<br>
</div>>                 print filename<br>
<br>
I was unable to run that code:<br>
<br>
$ cat testit.py<br>
<br>
for filename in files:<br>
<div class="im">    if (any(filename.endswith(x) for x in extensions)):<br>
</div>        print filename<br>
<br>
$ python testit.py<br>
<br>
Traceback (most recent call last):<br>
  File "testit.py", line 1, in <module><br>
    for filename in files:<br>
NameError: name 'files' is not defined<br>
<div class="im"><br>
> However, it will print all the files in list 'files' (that is all<br>
> files with file extension '.hdf'). My question is why it doesn't just<br>
> print the filename with extensions '.hdf5'?<br>
<br>
</div>Dunno.  You didn't provide enough information for us to answer your<br>
question: the code you posted won't run and don't tell us what values<br>
you're using for any of the variables.<br>
<br>
Here's a piece of runnable code that I think does what you want:<br>
<br>
$ cat testit.py<br>
files = ["foo.bar", "foo.baz", "foo.bax"]<br>
extensions = [".baz",".spam",".eggs"]<br>
<br>
for filename in files:<br>
<div class="im">    if (any(filename.endswith(x) for x in extensions)):<br>
</div>        print filename<br>
<br>
$ python testit.py<br>
foo.baz<br>
<br>
--<br>
<font color="#888888">Grant<br>
</font><div><div></div><div class="h5">--<br>
<a href="http://mail.python.org/mailman/listinfo/python-list" target="_blank">http://mail.python.org/mailman/listinfo/python-list</a><br>
</div></div></blockquote></div><br>