1

I have achieved writing all the things I needed to the text file, but essentially the program needs to keep going back to the text file and saving only the changes. At the moment it overwrites the entire file, deleting all the previous information.

tshepang
  • 12,111
  • 21
  • 91
  • 136
jdoe
  • 11
  • 2
  • 1
    Possible duplicate: http://stackoverflow.com/questions/4706499/how-do-you-append-to-file-in-python – sgp Jul 21 '14 at 19:56
  • Are the changes appended to the end? If not, is the changed content of the same length, in bytes, as the content that it replaces? – John1024 Jul 21 '14 at 20:01

3 Answers3

2

There is typical confusion about how are text files organized.

Text files are not organized by lines, but by bytes

When one looks to a text file, it looks like lines.

It is natural to expect, that on disk it goes the same way, but this is not true.

Text file are written to disk byte by byte, often one character being represented by one byte (but in some cases more bytes). A line of text happens to be just a sequence of bytes, being terminated by some sort of new lines ("\n", "\n\r" or whatever is used for new line).

If we want to change 2nd line out of 3, we would have to fit the change just in the bytes, used for 2nd line, not to mess up with line 3. If we would write too many bytes for line 2, we would overwrite bytes of line 3. If we would write too few bytes, there would be stil present some (alredy obsolete) bytes from remainder of line 2.

Strategies to modify content of text file

Republisher - Read it, modify in memory, write all content completely back

This might first sound like vasting a lot of effort, but it is by far the most often used approach and is in 99% most effective one.

The beauty is, it is simple.

The fact is, for most files sizes it is fast enouhg.

Journal - append changes to the end

Rather rare approach is to write first version of the file to the disk and later on append to the end notes about what has changed.

Reading such a file means, it has to rerun all the history of changes from journal to find out final content of the file.

Surgeon - change only affected lines

In case you keep lines of fixed length (measured in bytes!! not in characters), you might point to modified line and rewrite just that line.

This is quite difficult to do easily and is used rather with binary files. This is definitely not the task for beginers.

Conclusions

Go for "Republisher" pattern.

Use whatever format fits your needs (INI, CSV, JSON, XML, YAML...).

Personally I prefer saving data to JSON format - json package is part of Python stdlib and it supports lists as well dictionaries, what allows saving tabular as well as tree like structures.

Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
0

Are the changes you are making going to be over several different runs of a program? If not, I suggest making all of your changes to the data while it is still in memory and then writing it out just before program termination.

Julian Irwin
  • 172
  • 10
0

You can open it as follows:

FileOpen = open("test.txt","a")