[Tutor] search through a list

Light light_zls at 163.com
Sun Jul 3 09:59:51 CEST 2005

> import os
> #get a list of Logs in logs folder
> LogFiles = os.listdir('/home/nephish/Projects/Piv_GCI/logs')
> #open the loglist file and read the lines
> LogList = open('home/nephish/Projects/Piv/logs/LogList.txt', 'r')
> LogsRead = LogList.readlines()
> #see if there is  a new log file.
> for i in LogFiles:
>     if LogFiles[i] not in LogList:
>         #if there is a new log, open it and read the lines
>             StrName=(LogFiles[i])
>             NewLog =
> open('/home/nephish/Projects/Piv_CGI/logs'+StrName,'r')
> NewLogRead=NewLog.readlines()
>             #print the lines (testing only)
>             for x in NewLogRead:
>                 print x
>                 print '\n'
>             print '\n' #end for loop
>         else: #if the log file is not new,    
Hi, nephish.

I've read Brian's letter. He has given a solution for this problem. And 
below is another solution for it.

StrLog = " ".join([LogsRead[LogsRead.index(line)+1].strip() for line in 
LogsRead if line.strip() == i])

And I think 'LogFiles[i]' should be replaced by 'i', for 'i' is not an 
index but a string.


More information about the Tutor mailing list