[Tutor] search through a list
nephish
nephish at xit.net
Mon Jul 4 16:23:04 CEST 2005
Danny Yoo wrote:
>
>
>>#this function returns the number of lines on record for each log file.
>>def line_finder(LogsInDir, flag):
>> flag=flag.strip()
>> return_next=False
>> for line in LogsInDir:
>> if return_next:
>> return line.strip()
>> else:
>> if line.strip() == flag:
>> return_next = True
>>
>>
>
>
>Hi Nephish,
>
>Question: what should the program return if return_next is never True?
>That is, what happens if the program does not find the flag that you're
>looking for?
>
>The comment of this function also seems a bit weird: it appears to try to
>return the very next line after the 'flag' line of a file: it's not doing
>any counting of numbers, as far as I can tell.
>
>
>
>
>>the line in question is this one:
>>
>>NumLinesOnFile=line_finder(LogsRead, LogsInDir[NextLog])
>>print NumLinesOnFile
>>
>>if i run this in idle printing NumLinesOnFile gives me a string 14
>>
>>
>
>The function will return None if 'flag' can't be found in the LogsRead
>list. Otherwise, it'll return one of the lines of the file.
>
>So I'd assume for the moment that line_finder can't find what it is trying
>to find. You may want to test this yourself by adding some kind of
>debugging statement in line_finder() to make it more clear if t can't find
>what it's looking for. Does this sound reasonable?
>
>
>Best of wishes to you!
>
>
>
>
Ok, if the return_next is never True, returning None is very useful.
If it is true, yes, i do want it to return the next line of the list.
this is a list of files. It is a file name, followed by the number of
lines in the file,
this way i can only process the data that is new.
Your suggestion sounds reasonable and i will work with it with a few
more print statements to see exactly where it is failing.
maybe i had something messed up in my string that it is looking for that
wouldn't apply from IDLE.
Thanks for your help.
-shawn<><
More information about the Tutor
mailing list