I have a file that I want to get each line at a time, but once it gets to a specific line, I need to get the next few lines information.
Here is a code sample:
rofile = open('foo.txt', 'r')
for line in rofile:
print line
if(line.strip() == 'foo'):
line = line.next()
print line
line = line.next()
print line
line = line.next()
print line
When I come back around and loop for the second time, that first print statement should print the 5th line in the file. Is there any possible way to do this?
EDIT: Sorry for not clarifying the details. rofile
is a file object that I'm iterating through. Whether next()
is the real method to obtain the next line when using a file, I don't know. I don't have much experience with file manipulation in python.