Reading from text

O

oamram

Hi All,
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.
 
J

JB

oamram a écrit :
Hi All,
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).

First create a function that read and parse one file

Then create a loop that call this function for each file in a directory

Modules to read :

http://www.python.org/doc/2.5.2/tut/node9.html#SECTION009210000000000000000
http://docs.python.org/library/os.html


Julien
 
B

bearophileHUGS

oamram:
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).

Files can be iterated line-by-line, so this idiom:

for line in file: ...

will give you the lines, with newline. Once you have debugged that,
you can use the standard module glob (http://docs.python.org/library/
glob.html#module-glob ) to iterate on files.

If you have more problems, show use the code you have written and we
may suggest improvements.

Bye,
bearophile
 
M

MRAB

oamram:

Files can be iterated line-by-line, so this idiom:

for line in file: ...

will give you the lines, with newline. Once you have debugged that,
you can use the standard module glob (http://docs.python.org/library/
glob.html#module-glob ) to iterate on files.
[snip]
Or:

for index, line in enumerate(my_file): ...

where index will give you the line number (starting from 0, so you'll
want lines 6 to 10). You can break out of the loop when you have all the
lines you want.
 
T

Tim Chase

Assuming this is a real task and not a homework problem, then
I'd do it this way:

$ cd [directory containing 50 test files]
$ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt

I'd use sed:

sed -ns 7,11p /source/path/*.txt >/path/to/results.txt

hard to get much more concise than that with any common tool :)

-tkc
 
A

Aahz

Assuming this is a real task and not a homework problem, then
I'd do it this way:

$ cd [directory containing 50 test files]
$ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt

I'd use sed:

sed -ns 7,11p /source/path/*.txt >/path/to/results.txt

hard to get much more concise than that with any common tool :)

But you do have to learn sed. Avoiding that is precisely why I stick
with Python.
 
P

Paul McGuire

Hi All,
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).

import glob
file("output.txt","w").write('\n'.join( "".join(f.readlines()
[7:11+1]) ) for f in glob.glob("targetdir/*.txt"))

-- Paul
 
S

Steve Holden

Aahz said:
Assuming this is a real task and not a homework problem, then
I'd do it this way:

$ cd [directory containing 50 test files]
$ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt
I'd use sed:

sed -ns 7,11p /source/path/*.txt >/path/to/results.txt

hard to get much more concise than that with any common tool :)

But you do have to learn sed. Avoiding that is precisely why I stick
with Python.

+1 [cross-platform] QOTW
 
S

Steve Holden

Aahz said:
Assuming this is a real task and not a homework problem, then
I'd do it this way:

$ cd [directory containing 50 test files]
$ (for file in *; do head -n11 $file | tail -n5; done) >/path/to/results-file.txt
I'd use sed:

sed -ns 7,11p /source/path/*.txt >/path/to/results.txt

hard to get much more concise than that with any common tool :)

But you do have to learn sed. Avoiding that is precisely why I stick
with Python.

+1 [cross-platform] QOTW
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top