use fileinput to read a specific line

Russ P. Russ.Paielli at gmail.com
Tue Jan 8 01:08:54 EST 2008


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

Good point. I didn't think of that. It could also be done as follows:

for fileN in files:

    lnum = 0 # line number
    input = file(fileN)

    for line in input:
        lnum += 1
        if lnum >= 4: break

    input.close()

    # do something with "line"

Six of one or half a dozen of the other, I suppose.



More information about the Python-list mailing list