Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   file find skips first letter (http://www.velocityreviews.com/forums/t743575-file-find-skips-first-letter.html)

Wanderer 02-15-2011 06:32 PM

file find skips first letter
 
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.

Mel 02-15-2011 06:48 PM

Re: file find skips first letter
 
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.


Alexander Kapps 02-15-2011 06:55 PM

Re: file find skips first letter
 
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

Tim Chase 02-15-2011 06:59 PM

Re: file find skips first letter
 
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





MRAB 02-15-2011 07:11 PM

Re: file find skips first letter
 
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:


All times are GMT. The time now is 09:32 PM.

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