use fileinput to read a specific line

Scott David Daniels Scott.Daniels at Acm.Org
Tue Jan 8 08:25:48 EST 2008


Russ P. wrote:
> On Jan 7, 9:41 pm, Dennis Lee Bieber <wlfr... at ix.netcom.com> wrote:
>>         Given that the OP is talking 2000 files to be processed, I think I'd
>> recommend explicit open() and close() calls to avoid having lots of I/O
>> structures floating around...
>>
[effectively]
>> for fid in file_list:
>>         fin = open(fid)
>>         for cnt in xrange(4):
>>                 ln = fin.readline()
>>         fin.close()
> One second thought, I wonder if the reference counting mechanism would
> be "smart" enough to automatically close the previous file on each
> iteration of the outer loop. If so, the files don't need to be
> explicitly closed.

I _hate_ relying on that, but context managers mean you don't have to.
There are good reasons to close as early as you can.  For example,
readers of files from zip files will eventually either be slower or
not work until the other readers close.

Here is what I imagine you want (2.5 or better):

     from __future__ import with_statement

     def pairing(names, position):
         for filename in names:
             with open(filename) as f:
                 for n, line in enumerate(f):
                     if n == position:
                         break
                 else:
                     line = None  # indicate a short file
             yield filename, line
     ...
     for name, line in pairing(glob.glob('*.txt'), 3):
          do_something(name, line)

--Scott David Daniels
Scott.Daniels at Acm.Org



More information about the Python-list mailing list