Data inside text file:
GOPU 433 33332.000000
GOPAL 20 22233.000000
RAMU 33 76532.000000
Here is a code to modify data inside text file:
def modify():
another='Y'
while another=='Y':
print "\nEnter name of employee to modify"
empname=raw_input()
import os
fp.seek(0,os.SEEK_SET)
for line in fp:
if len(line)>1: #To handle line feed
e.name, e.age, e.bs=line.split()
e.age = int(e.age) # convert age from string to int
e.bs = float(e.bs)
if(cmp(e.name,empname)==0):
c=len(line)
print "\nEnter new name,age & bs"
e.name=raw_input()
e.age=eval(raw_input())
e.bs=eval(raw_input())
import os
fp.seek(-c,os.SEEK_CUR)
ch="%s %d %f" % (e.name,e.age,e.bs)
fp.writelines("\n")
fp.writelines(ch)
fp.writelines("\n")
break
print "\nModify another Record(Y/N)"
another=raw_input()
OUTPUT:
Enter name of employee to modify: GOPAL
Enter new name,age & bs: PANDEY
24
38374
file content becomes:
GOPU 433 33332.000000
GOPAL 20 22233.000000
PANDEY 24 38374
When I am trying to modify the data of GOPAL
then it is modifying the data of next employee i.e; RAMU
.
Didn't know why it is happening?
Please provide me a solution for this?