Reading from text
google at mrabarnett.plus.com
Tue Feb 17 13:00:32 EST 2009
bearophileHUGS at lycos.com wrote:
>> 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.
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.
> If you have more problems, show use the code you have written and we
> may suggest improvements.
More information about the Python-list