Has anyone ever repeatedly wrote to the same text file, using the append mode, and when examining the text file, several lines were missing?

Here are the specifics to my code:

  1. Each line in the output, the text file, contains data from one experimental session (e.g. the session time, experimenter name, session #, a series of responses to variables being tested)

  2. When I first write to the text file, I create a string variable that starts with "\n". Here's an example of the code:

    sessioninfo = "\n" + "," + string_variables + ","
    fp=open("data.txt", "a")
  3. During the course of the experiment, the program opens the text file and writes more information (e.g. the study variables). Here's an example below:

    dependent_variables = string_varaibles + ","

    fp=open("data.txt", "a")

  4. The last time I write to the file (for each experimental session) the code looks just like #3.

During cleaning and creation of the data base, I noticed that there are missing lines in the data file. Could my program have written over a line in the "data.txt"? Could the program start writing to the text file at a point that is not the true end? (Thus resulting in overwriting.)

I only have whole lines of data missing. There are no lines where the file was written to in the middle of a line.

Also, I understand that the append mode should start at the end to begin writing. The other options to use with the open command, which I don’t use, could any of those be the cause? Basically, I need to know if my program could potentially do this again?

I have already figured out alternatives to my original program, but would still like to know why this happened.

Any and all feedback is appreciated. I am not a programmer by trade, so please take that into consideration when responding (i.e. keep it as simple as you can).

Thank you!

3 Years
Discussion Span
Last Post by Social_psych

It is very unlikely, that open and write does not work properly.
More likely is that your program misses the line with the write method call, or you open the file twice without flushing the first time.
Consider this example:

>>> fp=open("a.txt","a")
>>> fp.write("asdf")
>>> fc=open("a.txt","a")
>>> fc.close()
>>> fp.close()

Edited by slate


No "/n". The data is there it's just on one r e a l l y long line.

fp=open("data.txt", "a")

And it should be

fp=open("data.txt", "a")
## do the input or calcs or whatever
## do more input and/or calcs
## at the end of the program

Thanks for the suggestions.

Here's a little more information, the write commands are embedded in functions that are embedded in a GUI. So, when I write to the text file, I use local variables in that specific function. My alternative is to create global variables of the needed output and write all the session data at the end (instead of periodically thru the experiment). Does this information change anything?

Also, I have literally written to a texts file this way about 1000x without any overwriting issues. I'm actually trying to rule out programming errors as the source of the missing data before I turn to other possible causes.

Slate - I assume by "flushing" you mean using the closing command after each write. It's there.

To woooee, the only time I use the new line call is when I 1st write to the data file for each new session (i.e. each new session should be one line in the data file). So, each experimental session in the data file is a really long line. However, the missing data are not in another line of data, say at the end or the middle. I don't understand what you mean by No new line.

Again, thanks for your feedback. If you have any other thoughts, please share.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.