use fileinput to read a specific line
jo3c
JO3chiang at gmail.com
Tue Jan 8 02:01:35 EST 2008
On Jan 8, 2:08 pm, "Russ P." <Russ.Paie... at gmail.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...
>
> 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.
this is what i did using glob
import glob
for files in glob.glob('/*.txt'):
x = open(files)
x.readline()
x.readline()
x.readline()
y = x.readline()
# do something with y
x.close()
More information about the Python-list
mailing list