Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > file find skips first letter

Reply
Thread Tools

file find skips first letter

 
 
Wanderer
Guest
Posts: n/a
 
      02-15-2011
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.
 
Reply With Quote
 
 
 
 
Mel
Guest
Posts: n/a
 
      02-15-2011
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.

 
Reply With Quote
 
 
 
 
Alexander Kapps
Guest
Posts: n/a
 
      02-15-2011
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
 
Reply With Quote
 
Tim Chase
Guest
Posts: n/a
 
      02-15-2011
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




 
Reply With Quote
 
MRAB
Guest
Posts: n/a
 
      02-15-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:
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Samsung DVD-1080P7 skips first few seconds of chapters eduardorp1@gmail.com DVD Video 0 05-22-2007 06:43 AM
File-Find skips directories with spaces depending on path separator on Windows Matt Garrish Perl Misc 9 01-17-2006 02:12 PM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
big letter -> small letter vertigo Python 4 07-06-2004 07:23 AM
RE: big letter -> small letter Tony Meyer Python 0 07-06-2004 07:11 AM



Advertisments