next()
in python does not work. What is an alternative to reading next line in Python? Here is a sample:
filne = "D:/testtube/testdkanimfilternode.txt" f = open(filne, 'r+') while 1: lines = f.readlines() if not lines: break for line in lines: print line if (line[:5] == "anim "): print 'next() ' ne = f.next() print ' ne ',ne,'n' break f.close()
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:
filne = "in" with open(filne, 'r+') as f: lines = f.readlines() for i in range(0, len(lines)): line = lines[i] print line if line[:5] == "anim ": ne = lines[i + 1] # you may want to check that i < len(lines) print ' ne ',ne,'n' break