[Tutor] Searching through files for values

Jason Brown zerokey at gmail.com
Fri Aug 14 01:48:52 CEST 2015


Hi,

I'm trying to search for list values in a set of files.  The goal is to
generate a list of lists that can later be sorted.  I can only get a match
on the first value in the list:

contents of value_file:
value1
value2
value3
...

The desired output is:

file1 value1
file1 value2
file2 value3
file3 value1
...

Bit it's only matching on the first item in vals, so the result is:

file1 value1
file3 value1

The subsequent values are not searched.

filenames = [list populated with filenames in a dir tree]
vals = []
value_file = open(vars)
for i in value_file:
    vals.append(i.strip())
    value_file.close()

for file_list in filenames:
    with open(file_list) as files:
         for items in vals:
             for line in files:
                 if items in line:
                     print file_list, line



for line in vals:
    print line

returns:
['value1', 'value2', 'value3']

print filenames

returns:
['file1', 'file2', 'file3']


Any help would be greatly appreciated.

Thanks,

Jason


More information about the Tutor mailing list