[Tutor] How to skip to next line in for loop

Alan Gauld alan.gauld at btinternet.com
Fri May 2 01:48:02 CEST 2008


"Brain Stormer" <brnstrmrs at gmail.com> wrote 

> f = open('file.txt',r)
> for line in f.read():

This reads the whole file inta a string then iterates 
over all the characters(not lines) in that string.

Better to iterate over the file:

for line in f:


>     if line == "3":
>           line.next

this then becomes f.next()  # next is a method not 
an attribute so needs the () to call it


-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld



More information about the Tutor mailing list