question about endswith()

Discussion in 'Python' started by Matt Funk, Mar 3, 2011.

  1. Matt Funk

    Matt Funk Guest

    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
     
    Matt Funk, Mar 3, 2011
    #1
    1. Advertising

  2. On 2011-03-03, Matt Funk <> 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
     
    Grant Edwards, Mar 3, 2011
    #2
    1. Advertising

  3. Matt Funk

    Matt Funk Guest

    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 <> 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.
    >
     
    Matt Funk, Mar 4, 2011
    #3
  4. On 2011-03-04, 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


    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
     
    Grant Edwards, Mar 4, 2011
    #4
  5. 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
     
    Jean-Michel Pichavant, Mar 4, 2011
    #5
  6. Matt Funk

    HMX962b Guest

    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
     
    HMX962b, Mar 4, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    17
    Views:
    1,913
    Chris Uppal
    Nov 16, 2005
  2. Michele Simionato

    feature request: a better str.endswith

    Michele Simionato, Jul 18, 2003, in forum: Python
    Replies:
    24
    Views:
    798
    Peter Hansen
    Jan 9, 2004
  3. metaperl
    Replies:
    5
    Views:
    314
    Lawrence D'Oliveiro
    Sep 29, 2006
  4. =?utf-8?B?Qm9yaXMgRHXFoWVr?=

    Significance of "start" parameter to string method "endswith"

    =?utf-8?B?Qm9yaXMgRHXFoWVr?=, Apr 19, 2007, in forum: Python
    Replies:
    4
    Views:
    444
    John Machin
    Apr 19, 2007
  5. =?utf-8?B?Qm9yaXMgRHXFoWVr?=
    Replies:
    5
    Views:
    344
    Steven D'Aprano
    Apr 21, 2007
Loading...

Share This Page