[Tutor] reading lines from a list of files

Albert-Jan Roskam fomcl at yahoo.com
Tue May 12 20:48:23 CEST 2015


------------------------------
On Tue, May 12, 2015 8:48 AM CEST 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.

It was not that long ago that I found out about the fileinput module, so I sometimes forget to use it. It is not specify the encoding of the files, is it? It'd be nice if one could specify a tuple of encodings, e.g. ('utf-8-sig', 'latin1', 'cp1252'). 

input(files=None, inplace=0, backup='', bufsize=0, mode='r', openhook=None)
        input([files[, inplace[, backup[, mode[, openhook]]]]])


More information about the Tutor mailing list