question about endswith()

M

Matt Funk

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
 
G

Grant Edwards

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.
 
M

Matt Funk

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
 
G

Grant Edwards

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
 
J

Jean-Michel Pichavant

Matt said:
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
 
H

HMX962b

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top