Python File append error
Question: This code works fine for me. Appends data at the end.

    
    
    def writeFile(dataFile, nameFile):
        fob = open(nameFile,'a+')
        fob.write("%s\n"%dataFile)
        fob.close()
    

But the problem is when I close the program and later run again I found that
all the previous data were lost. Process is started to write from the start
and there is no data in the file.

But during the run it perfectly add a line at the end of file.

I can't understand the problem. Please some one help.

NB: I am using Ubuntu-10.04 with python 2.6


Answer: There is nothing wrong with the code you posted here... I tend to agree with
other the comments that this file is probably being overwritten elsewhere in
your code.

The only suggestion I can think of to test this explicitly (if your use case
can tolerate it) is to throw in an exit() statement at the end of the function
and then open the file externally (aka in gedit) and see if the last change
took.

Alternatively to the exit, you could run the program in the terminal and
include a call to pdb at the end of this function which would interrupt the
program without killing it:

    
    
    import pdb; pdb.set_trace()
    

You will then have to hit c to continue the program each time this runs.

If that checks out, do a search for other places this file might be getting
opened.

