[Tutor] Reading every 5th line

Steven D'Aprano steve at pearwood.info
Sun Aug 8 17:18:41 CEST 2010


On Sun, 8 Aug 2010 10:04:21 pm nitin chandra wrote:
> Hello Everyone,
>
> I am to make a small programme for a friend of mine
> where i am to start reading from 14th (string) from a file and then
> read every 5th row.

There are many variations, here's one:

Write a loop that executes 14 times. Inside that loop, read one line 
from the input file, and write it to the output file.

Now write a second loop that executes forever. (Don't worry, you'll 
break out of the loop at some point.) Inside the loop, read one line 
from the input file, and write it to the output file. Then read four 
more lines, and throw them away. If you read a line, and it's empty, 
then you've reached End Of File (EOF) and you can break out of the 
loop.


-- 
Steven D'Aprano


More information about the Tutor mailing list