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