[Tutor] reading lines from a list of files

Alex Kleider akleider at sonic.net
Thu May 14 10:01:39 CEST 2015


On 2015-05-13 23:24, Danny Yoo wrote:
>> As a follow up question:
>> The following seems to work-
>> 
>>     for f_name in list_of_file_names:
>>         for line in open(f_name, 'r'):
>>             process(line)
>> 
>> but should I be worried that the file doesn't get explicitly closed?
> 
> 
> It depends on context.  Personally, I'd write it with the 'with' to
> make it very clear that the loop will manage its resource.  That being
> said, it sounds like there might be concerned about the nesting.
> We're nesting three or four levels deep, at the very least!
> 
> I'd agree with that.  Because of this, it might be worthwile to
> consider refactoring the processing of the file in a separate
> function, something like this:
> 
> ###############################
> def processFile(f):
>     for line in f:
>         ...
> 
> for f_name in list_of_file_names:
>      with open(f_name, 'r') as f:
>          processFile(f)
> ###############################
> 
> The primary reason is to reduce the nesting.  But there's also a
> potential side benefit: processFile() has a better chance of being
> unit-testable, since we can pass in instances of other file-like
> objects, such as io.StringIO(), to spot-check the behavior of the
> process.

Thanks, Danny.  This is particularly germane since the issue has come up 
in the
midst of my first attempt to do test driven development.  I'll try 
refactoring per
your suggestion.


More information about the Tutor mailing list