Skip to content
Advertisement

Python read next()

next() in python does not work. What is an alternative to reading next line in Python? Here is a sample:

JavaScript

Running this on a file does not show ‘ne ‘.

Advertisement

Answer

next() does not work in your case because you first call readlines() which basically sets the file iterator to point to the end of file.

Since you are reading in all the lines anyway you can refer to the next line using an index:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement