[Tutor] reading lines from a list of files

Alex Kleider akleider at sonic.net
Thu May 14 07:27:11 CEST 2015


> 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.

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?

Alex


More information about the Tutor mailing list