Updating mulitple lines of file in python I am writing automation script in python to update certain version in a file.
Here is the snippet from that file.
if "$(CURTARGET)" == "linux_rh_el6_x86_64_32"
/* See $VOB/FMSrules.mk */
GOLDEN_CPPHDR_VER=4.4.7
GOLDEN_CPPBKW_VER=3.4.6
GOLDEN_LIBHDR_VER=$(GOLDEN_CPPHDR_VER)
RHEL_KERNEL=2.6.32-696.16.1.el6.x86_64
    end
    if "$(CURTARGET)" == "linux_rh_el7_x86_64_32"
/* See $VOB/FMSrules.mk */
GOLDEN_CPPHDR_VER=4.8.5
GOLDEN_CPPHDR_VER=4.4.7
GOLDEN_LIBHDR_VER=$(GOLDEN_CPPHDR_VER)
RHEL_KERNEL=3.10.0-693.el7.x86_64
    end

I have to update GOLDEN_CPPHDR_VER,GOLDEN_CPPHDR_VER & RHEL_KERNEL based on the value of $(CURTARGET).
AS you can see the 3 variables GOLDEN_CPPHDR_VER,GOLDEN_CPPHDR_VER & RHEL_KERNEL are not unique and i cannot directly search and update the same.
I tried coding it in this way.
def update_fmsrules():

    print " \n\n--- Updating FMSrules.mk ----\n"
    f = open(FMSrules_file,'r')
    lines = []
    while True:
try:
line= f.readline()
#print line
if (re.search(r'\blinux_rh_el7_x86_64_32\b',line)):
print "matched\n"
nextline = f.next()
print nextline
if "GOLDEN_CPPHDR_VER" in nextline:
print "entered if"
print line +"\ntrue\n"+ nextline
print f.tell()
break

except StopIteration:
print "exception"
return


    f.close()

But it is not working as expected.
print "matched\n"

I am seeing the above output.
nextline = f.next()
print nextline

But above line is not printing actual nextline but some garbage lines.
And the code is running infinitely.
what is the best way of doing this using python?
How can i do it using file operation in python?