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

Kent Johnson kent37 at tds.net
Thu May 1 23:15:57 CEST 2008


On Thu, May 1, 2008 at 5:04 PM, Brain Stormer <brnstrmrs at gmail.com> wrote:
> I have the following code:
>
> f = open('file.txt',r)
> for line in f.read():
>      if line == "3":
>            line.next

Try
  f.next()

line is a string, it doesn't have a next() method. The file itself is
iterable and has next(). Don't forget the parentheses!

Kent

>            print line
> f.close()
>
> The file.txt looks like
> 1
>  2
> 3
> 4
> 5
>
> I would like the code to output "4"
>
> but I don't know how to use the next.  Are there any other way?
>
> Thanks
>
>
>
>
> _______________________________________________
>  Tutor maillist  -  Tutor at python.org
>  http://mail.python.org/mailman/listinfo/tutor
>
>


More information about the Tutor mailing list