1

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

3
  • Sounds like elsewhere in your program you are overwriting the file. Commented May 3, 2013 at 14:14
  • 1
    Is there another part of thw program which does open(thesameFile, 'w')? Commented May 3, 2013 at 14:14
  • Yes I was. Thanks. I wrote this function as module. But some part of my code I did not replaced that line. Commented May 3, 2013 at 16:37

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, David You are my life saver. I found the bug by the help of your suggestion to debug.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.