[Tutor] reading lines from a list of files

Peter Otten __peter__ at web.de
Tue May 12 12:46:32 CEST 2015


Alex Kleider wrote:

> On 2015-05-11 23:48, Peter Otten wrote:
>> Alex Kleider wrote:
>> 
>>> Is there a better (more 'Pythonic') way to do the following?
>>> 
>>>      for f_name in f_names:
>>>          with open(f_name, 'r') as f:
>>>              for line in f:
>> 
>> There's the fileinput module
>> 
>> <https://docs.python.org/dev/library/fileinput.html#fileinput.input>
>> 
>> but personally I prefer the way you show above.
> 
> Then I'll stick with what you prefer and what I know.
> It seems silly to import yet another module for the sole
> purpose of saving one line of code 

I think of importing a module as "cheap" unless it draws in a framework (e. 
g. numpy). And don't forget that even small pieces of code should be tested. 
So you aren't just saving the extra line, but also some of your tests.

> although the reason
> for my inquiry was more to diminish levels of indentation
> than number of lines.

You usually do that by factoring out the loops into a generator:

def lines(files):
    for file in files:
        with open(files) as f:
            yield from f  # before python 3.3: for line in f: yield line


for line in lines(files):
    ...


Also possible, but sloppy as files are closed on garbage collection rather 
than explicitly:

lines = (line for file in files for line in open(file))
for line in lines:
   ...




More information about the Tutor mailing list