iterating through files

O

oamram

Hi Pythonist,
new to python. i have a directory with about 50 text file and i need to
iterate through them and get
line 7 to 11 from each file and write those lines into another file(one file
that will contain all lines).

Cheers, Omer.
 
M

Mike Driscoll

Hi Pythonist,
new to python. i have a directory with about 50 text file and i need to
iterate through them and get
line 7 to 11 from each file and write those lines into another file(one file
that will contain all lines).

Cheers, Omer.

I would recommend using the glob module to grab a list of the files
you want or you can just create your own list. Then use a loop to grab
the lines you want. Something like this:

f = open(textFile)
newFile = open(newFileName, "a")
x = 1
for line in f.readlines():
if x >=7 and x <=11:
newFile.write(line + "\n")

You could even put the above inside a loop that loops over the list of
files. Anyway, that's one approach. I'm sure there are many others.

Mike
 
M

Mike Driscoll

I would recommend using the glob module to grab a list of the files
you want or you can just create your own list. Then use a loop to grab
the lines you want. Something like this:

f = open(textFile)
newFile = open(newFileName, "a")
x = 1
for line in f.readlines():
    if x >=7 and x <=11:
         newFile.write(line + "\n")

You could even put the above inside a loop that loops over the list of
files. Anyway, that's one approach. I'm sure there are many others.

Mike

Oops...I forgot to iterate the counter. The code should look like
this:

<code>

f = open(textFile)
newFile = open(newFileName, "a")
x = 1
for line in f.readlines():
if x >=7 and x <=11:
newFile.write(line + "\n")
x +=1

</code>

Sorry about that.

Mike
 
C

Chris Rebert

Oops...I forgot to iterate the counter. The code should look like
this:

<code>

f = open(textFile)
newFile = open(newFileName, "a")
x = 1
for line in f.readlines():
if x >=7 and x <=11:
newFile.write(line + "\n")
x +=1

</code>

Or you could use enumerate(); also, readlines() isn't necessary:

f = open(textFile)
newFile = open(newFileName, "a")
for x, line in enumerate(f):
if x >=7 and x <=11:
newFile.write(line + "\n")

Sounds a bit like homework to me though...

Cheers,
Chris
 
S

Steve Holden

Chris said:
Or you could use enumerate(); also, readlines() isn't necessary:

f = open(textFile)
newFile = open(newFileName, "a")
for x, line in enumerate(f):
if x >=7 and x <=11:
newFile.write(line + "\n")

Sounds a bit like homework to me though...
But all these solutions read the whole file. You should really read six
lines throwing them away then read five lines to keep.

Since this might be homework I'll not write the code, but it will
involve f.next() ...

regards
Steve
 
M

Mensanator

But all these solutions read the whole file.  You should really read six
lines throwing them away then read five lines to keep.

It might be useful not to assume every file has at least 11 lines
and program accordingly.
 
M

Marc 'BlackJack' Rintsch

new to python. i have a directory with about 50 text file and i need to
iterate through them and get
line 7 to 11 from each file and write those lines into another file(one
file that will contain all lines).

Untested:

from __future__ import with_statement
from glob import glob
from itertools import islice


def main():
with open('result.txt', 'w') as out_file:
for filename in glob('foo/*.txt'):
with open(filename, 'r') as lines:
out_file.writelines(islice(lines, 7, 12))


if __name__ == "__main__":
main()


Ciao,
Marc 'BlackJack' Rintsch
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,297
Messages
2,571,529
Members
48,249
Latest member
reactnativeexpert

Latest Threads

Top