start reading from certain line

A.T.Hofkamp hat at
Wed Jul 9 13:09:09 CEST 2008

On 2008-07-09, antar2 <desothier at> wrote:
> I am a starter in python and would like to write a program that reads
> lines starting with a line that contains a certain word.
> For example the program starts reading the program when a line is
> encountered that contains 'item 1'
> The weather is nice
> Item 1
> We will go to the seaside
> ...
> Only the lines coming after Item 1 should be read

Not possible at most OSes, file reading always starts at the first character at
the first line. Also, most OSes don't understand the 'line' concept natively, a
file is just a long sequence of characters to them (end-of-line is also just a
character, namely '\n' (or '\r\n' if you are using Windows).

So you have to read the entire file, then throw away the bits you don't want to
keep. Luckily, Python does understand what a 'line' is, which makes the prblem

Have a look at the readline() function (or the readlines() function if your
file is not too long). That should give you a start.


More information about the Python-list mailing list