file find skips first letter

Discussion in 'Python' started by Wanderer, Feb 15, 2011.

  1. Wanderer

    Wanderer Guest

    I'm using code

    def getFiles(self, fileBase):
    """return a list of the filenames in a director containing a
    base word
    """

    allFiles = os.listdir(self.resultDir)
    baseFiles = []
    for f in allFiles:
    if f.find(fileBase) > 0:
    baseFiles.append(f)

    return baseFiles

    but the code can't find files with fileBase in it if the fileBase
    starts the filename.

    if the filenames are rnoise##.tif and fileBase is "rnoise" the file
    won't be found. If fileBase is "noise" the files will be found.
     
    Wanderer, Feb 15, 2011
    #1
    1. Advertising

  2. Wanderer

    Mel Guest

    Wanderer wrote:

    > I'm using code
    >
    > def getFiles(self, fileBase):
    > """return a list of the filenames in a director containing a
    > base word
    > """
    >
    > allFiles = os.listdir(self.resultDir)
    > baseFiles = []
    > for f in allFiles:
    > if f.find(fileBase) > 0:
    > baseFiles.append(f)
    >
    > return baseFiles
    >
    > but the code can't find files with fileBase in it if the fileBase
    > starts the filename.
    >
    > if the filenames are rnoise##.tif and fileBase is "rnoise" the file
    > won't be found. If fileBase is "noise" the files will be found.


    (untested) Try

    if f.find(fileBase) > -1:


    Mel.
     
    Mel, Feb 15, 2011
    #2
    1. Advertising

  3. On 15.02.2011 19:32, Wanderer wrote:
    > I'm using code
    >
    > def getFiles(self, fileBase):
    > """return a list of the filenames in a director containing a
    > base word
    > """
    >
    > allFiles = os.listdir(self.resultDir)
    > baseFiles = []
    > for f in allFiles:
    > if f.find(fileBase)> 0:
    > baseFiles.append(f)
    >
    > return baseFiles
    >
    > but the code can't find files with fileBase in it if the fileBase
    > starts the filename.
    >
    > if the filenames are rnoise##.tif and fileBase is "rnoise" the file
    > won't be found. If fileBase is "noise" the files will be found.


    str.find() returns the index to the left-most occurrence or -1 if
    the substring is not found. So, if the file name starts with
    fileBase, find() return 0 which you filter out with your test
    f.find(fileBase)> 0.

    Either use f.find(fileBase) >= 0 or better:

    baseFiles = []
    for f in allFiles:
    if fileBase in f:
    baseFiles.append(f)


    HTH
     
    Alexander Kapps, Feb 15, 2011
    #3
  4. Wanderer

    Tim Chase Guest

    On 02/15/2011 12:32 PM, Wanderer wrote:
    > if f.find(fileBase)> 0:


    ..find() returns "-1" on failure, not 0. You want ">=" instead of
    just ">", or even more readably

    if fileBase in f:

    -tkc
     
    Tim Chase, Feb 15, 2011
    #4
  5. Wanderer

    MRAB Guest

    On 15/02/2011 18:48, Mel wrote:
    > Wanderer wrote:
    >
    >> I'm using code
    >>
    >> def getFiles(self, fileBase):
    >> """return a list of the filenames in a director containing a
    >> base word
    >> """
    >>
    >> allFiles = os.listdir(self.resultDir)
    >> baseFiles = []
    >> for f in allFiles:
    >> if f.find(fileBase)> 0:
    >> baseFiles.append(f)
    >>
    >> return baseFiles
    >>
    >> but the code can't find files with fileBase in it if the fileBase
    >> starts the filename.
    >>
    >> if the filenames are rnoise##.tif and fileBase is "rnoise" the file
    >> won't be found. If fileBase is "noise" the files will be found.

    >
    > (untested) Try
    >
    > if f.find(fileBase)> -1:
    >

    Python is a 0-based language, so if fileBase is at the start of f then
    the result is 0. If fileBase isn't in f then the result is -1.

    An alternative is:

    if fileBase in f:
     
    MRAB, Feb 15, 2011
    #5
    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. Jay McGavren
    Replies:
    11
    Views:
    1,140
    Alan Krueger
    Jan 16, 2006
  2. vertigo

    big letter -> small letter

    vertigo, Jul 6, 2004, in forum: Python
    Replies:
    4
    Views:
    759
    Reinhold Birkenfeld
    Jul 6, 2004
  3. Tony Meyer

    RE: big letter -> small letter

    Tony Meyer, Jul 6, 2004, in forum: Python
    Replies:
    0
    Views:
    507
    Tony Meyer
    Jul 6, 2004
  4. Andrew McNamara

    Re: big letter -> small letter

    Andrew McNamara, Jul 6, 2004, in forum: Python
    Replies:
    2
    Views:
    752
    Scott David Daniels
    Jul 6, 2004
  5. Matt Garrish
    Replies:
    9
    Views:
    168
    Matt Garrish
    Jan 17, 2006
Loading...

Share This Page