-1

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user3223301
  • 201
  • 4
  • 11

1 Answers1

3

You are looping over the file object as an iterator:

for line in fp:

which uses an internal read-ahead buffer; this means that using f.seek() works relative to where the buffer read to, not to the line you are processing right now.

You'd have to use f.readline() calls instead:

for line in iter(fp.readline, ''):

would give you the same loop effect without the read-ahead buffer.

Your approach will run into other problems however; any change in line size will either not overwrite enough or overwrite the next line too. A file will not shrink or grow automatically if the output you write to it does not exactly match the line length you are replacing.

Instead, you'll have to rewrite the whole file into a temp file and move that back into position.

Further remarks:

  • Use fp.write(); fp.writelines() will end up writing each character individually. It'll work, but only by chance and slowly.

  • Don't use cmp() when comparing strings. Just use string1 == string2.

  • Don't use eval() where int() or float() will do.

  • You only need to import a module just once, at the top. Those extra import os lines are redundant.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • now ,it's changing the data of the desired employee but when edited employee's name is too long then it's overwriting the some part of name of next employee...:( – user3223301 Feb 01 '14 at 11:43
  • 2
    @user3223301 that's the reason why you shouldn't modify text files like that... Martijn is telling you of this problem in his answer – Ricardo Cárdenes Feb 01 '14 at 11:49
  • Can you give me some hint of modifying data using temp file? The main thing is that i didn't want to change the sequence by modification i.e, it should modify data at its place instead of deleting and writing data at the end – user3223301 Feb 01 '14 at 12:10
  • See http://stackoverflow.com/questions/16485916/how-to-search-and-replace-in-a-file-using-python/16485997#16485997 for an example. – Martijn Pieters Feb 01 '14 at 13:05