[Tutor] search through a list
nephish
nephish at xit.net
Mon Jul 4 05:12:36 CEST 2005
Hey there, sorry to bother you again about this same problem
but i have a line of code in this same issue that works in idle
but it does not work in a regular script.
that same thing i am trying to do, reading the directory, comparing the
list of files to a list ..... just want to catch the newest lines of a
log file to process later.
here is what i have ( kinda long )
____________________________________________________________
#!/usr/bin/python
# Update Database
import os
print ' '
print ' '
#get a list of log files in the directory
LogsInDir=os.listdir('/home/nephish/Projects/Piv_CGI/logs')
print LogsInDir #testing only, remove later
#read the contents of the log list
LogList = open('/home/nephish/Projects/Piv_CGI/LogList.txt', 'r')
LogsRead=LogList.readlines()
LogList.close()
print 'Lines of LogList.txt'
for i in LogsRead:
print i #testing only, remove later
#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
NumLogsInDir = len(LogsInDir)
NextLog = 0
while NextLog < NumLogsInDir:
print 'opening file '+LogsInDir[NextLog]
print ' '
Count_in_Log =
open('/home/nephish/Projects/Piv_CGI/logs/'+LogsInDir[NextLog], "r")
Defer = Count_in_Log.readlines()
Count_in_Log.close()
CountEmUp = len(Defer)
print 'number is '
print CountEmUp
NumLinesOnFile=line_finder(LogsRead, LogsInDir[NextLog])
print NumLinesOnFile
if CountEmUp > int(NumLinesOnFile):
print " "+LogsInDir[NextLog]+" is longer than on record"
XtraLines = int(NumLinesOnFile) - CountEmUp
print XtraLines
else:
print ' '+LogsInDir[NextLog]+" is not greater than on record"
XtraLines=0
print XtraLines
NextLog=NextLog+1
print 'done'
______________________________________________________________________
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
when run in a terminal it returns "None"
dont get it. is it because it is in a while loop?
any ideas would be awesome here.
thanks
More information about the Tutor
mailing list