[Tutor] reading lines from a list of files
Danny Yoo
dyoo at hashcollision.org
Thu May 14 08:24:03 CEST 2015
> 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.
More information about the Tutor
mailing list