Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   question about endswith() (http://www.velocityreviews.com/forums/t744517-question-about-endswith.html)

Matt Funk 03-03-2011 11:39 PM

question about endswith()
 
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)):

The problem is that i let's all files though rather than just the hdf5
file. Is there anything i am doing wrong?

thanks
matt

Grant Edwards 03-03-2011 11:50 PM

Re: question about endswith()
 
On 2011-03-03, Matt Funk <mafunk@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.

--
Grant Edwards grant.b.edwards Yow! How's it going in
at those MODULAR LOVE UNITS??
gmail.com

Matt Funk 03-04-2011 12:18 AM

Re: question about endswith()
 
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@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.
>



Grant Edwards 03-04-2011 01:46 AM

Re: question about endswith()
 
On 2011-03-04, Matt Funk <mafunk@nmsu.edu> wrote:
> 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


I was unable to run that code:

$ cat testit.py

for filename in files:
if (any(filename.endswith(x) for x in extensions)):
print filename

$ python testit.py

Traceback (most recent call last):
File "testit.py", line 1, in <module>
for filename in files:
NameError: name 'files' is not defined

> 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'?


Dunno. You didn't provide enough information for us to answer your
question: the code you posted won't run and don't tell us what values
you're using for any of the variables.

Here's a piece of runnable code that I think does what you want:

$ cat testit.py
files = ["foo.bar", "foo.baz", "foo.bax"]
extensions = [".baz",".spam",".eggs"]

for filename in files:
if (any(filename.endswith(x) for x in extensions)):
print filename

$ python testit.py
foo.baz

--
Grant

Jean-Michel Pichavant 03-04-2011 10:08 AM

Re: question about endswith()
 
Matt Funk wrote:
> 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
>
>

Matt, in the code above your are iterating through the files, and if an
hdf5 is in the list, you print the current element. Since the hdf5 will
always be in that list, for each element you print it.

test.py:

import os

files = ['a.hdf', 'b.hdf5', 'c.hdf']
hdf5 = [_file for _file in files if os.path.splitext(_file)[1] == '.hdf5']
print hdf5



> python test.py

['b.hdf5']

JM


HMX962b 03-04-2011 02:52 PM

Re: question about endswith()
 
On 03/03/11 23:39, 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)):
>
> The problem is that i let's all files though rather than just the hdf5
> file. Is there anything i am doing wrong?



try

if filename.endswith('.hdf5'):


your code is testing filename for every (x) extension in a list of
extensions. and any() will return True if one matches the filename

--
djc


All times are GMT. The time now is 10:29 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.