[Tutor] Searching through files for values

Cameron Simpson cs at zip.com.au
Fri Aug 14 04:32:03 CEST 2015


On 13Aug2015 16:48, Jason Brown <zerokey at gmail.com> wrote:
>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.

Rhat is because the subsequent values are never loaded:

>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()

You close value_file inside the loop i.e. immediately after the first value.  
Because the file is closed, the loop iteration stops.  You need to close it
outside the loop (after all the values have been loaded):

    value_file = open(vars)
    for i in value_file:
        vals.append(i.strip())
    value_file.close()

It is worth noting that a better way to write this is:

    with open(vars) as value_file:
        for i in value_file:
            vals.append(i.strip())

Notice that there is no .close(). The "with" construct is the pynthon syntax to 
use a context manager, and "open(vars)" returns an open file, which is also a 
context manager. A context manager has enter and exit actions which fire 
unconditionally at the start and end of the "with", even if the with is exited 
with an exception or a control like "return" or "break".

The benefit of this is after the "with", the file will _always" get closed. It 
is also shorter and easier to read.

>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

I would remark that "file_list" is not a great variable name. Many people would 
read it as implying that its value is a list. Personally I would have just 
called it "filename", the singular of your "filenames".

Cheers,
Cameron Simpson <cs at zip.com.au>


More information about the Tutor mailing list