After I played a little in my terminal, I can tell what differences are on ubuntu linux 11.04 using python 2.7.1.
Opening with "w" truncates (ie deletes the contents) of the file immediately after opening it. In other words, by simply opening the file with open('file.txt', 'w') and going beyond the blank sheet.
Opening with 'a' leaves the contents of the file intact. Thus, opening with open('file.txt', 'a') and exiting leaves the file unchanged.
This also applies to update options for open. The open('file.txt', 'w+') command will leave an empty file, and the open('file.txt', 'r+') and open('file.txt', 'a+') will leave the files unchanged.
The difference between the "r +" and "a +" parameters is the behavior that others have suggested. The "r +" parameter allows you to write anywhere in the file, and "a +" forces everything to be written to the end of the file, regardless of where you set the current position of the file.
If you want to learn more about it, according to the python documentation , the open function accepts modes similar to the fopen function in C stdio.
source share