What is the difference between 'r +' and 'a +' when the open file is in python?


Possible Duplicate:
python open built-in function: difference between modes a, a+, w, w+, and r+?

I have try r+ and a+ to open file and read and write, but 'r+' and 'a+' are all append the str to the end of the file.

So, what's the difference between r+ and a+ ?


I have found the reason:

I have read the file object and forgot to seek(0) to set the location to the begin

Python opens files almost in the same way as in C:

  • r+ Open for reading and writing. The stream is positioned at the beginning of the file.

  • a+ Open for reading and appending (writing at end of file). The file is created if it does not exist. The initial file position for reading is at the beginning of the file, but output is appended to the end of the file (but in some Unix systems regardless of the current seek position).