Python uses the stdio fopen function and passes the mode as an argument. I assume you use windows, as @Lev says the code works fine on Linux.
Below is the fopen windows documentation, this may be the key to solving your problem:
When the access type is "r +", "w +" or "a +", both read and write are allowed (the file is considered open for "update"). However, when you switch between reading and writing, there should be fflush, fsetpos, fseek or rewind. An electric current position can be indicated for the operation fsetpos or fseek, if desired.
So the solution is to add file.seek() before calling file.write() . To add to the end of the file, use file.seek(0, 2) .
For your reference, file.seek works like this:
To reposition file objects, use f.seek (offset, from_what). The position is calculated by adding an offset to the reference point; the breakpoint is selected with the from_what argument. A from_what value of 0 measures from the beginning of the file, 1 uses the current position of the file, and 2 uses the end of the file as a control point. from_what can also be omitted by default 0, using the beginning of the file as a starting point.
[link: http://docs.python.org/tutorial/inputoutput.html]
As mentioned in the comments of @lvc and @Burkhan in his answer, you can use the new public function from io module . However, I want to note that the write function does not work exactly the same in this case - you need to specify unicode strings as input [just a u prefix for the string in your case]:
from io import open fil = open('text.txt', 'a+') fil.write('abc')
Finally, please avoid using the name βfileβ as the variable name, since it is an inline type and will silently overwrite, which leads to some errors that are difficult to detect.