file find skips first letter

MRAB python at mrabarnett.plus.com
Tue Feb 15 20:11:12 CET 2011


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:



More information about the Python-list mailing list